Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用hapi中编码的API_Javascript_Rest_Api_Post_Hapijs - Fatal编程技术网

Javascript 使用hapi中编码的API

Javascript 使用hapi中编码的API,javascript,rest,api,post,hapijs,Javascript,Rest,Api,Post,Hapijs,我正在使用一个没有文档记录的RESTful api。然而,它背后的代码是开源的。它似乎是用hapi和书架FJ的组合编写的。我正试图找出如何向其中一条路由发送POST请求,但它不起作用。以下是路线的代码: { method: 'POST', path: '/api/survey_answer', handler: (request, reply) => { if (request.payload.responses) { Promise.a

我正在使用一个没有文档记录的RESTful api。然而,它背后的代码是开源的。它似乎是用hapi和书架FJ的组合编写的。我正试图找出如何向其中一条路由发送POST请求,但它不起作用。以下是路线的代码:

{
    method: 'POST',
    path: '/api/survey_answer',
    handler: (request, reply) => {
      if (request.payload.responses) {
        Promise.all(
          request.payload.responses.map(function(answer) {
              var surveyAnswer = new SurveyAnswer()
              var newAnswer = surveyAnswer
              .save({
                survey_response_id: answer.surveyResponseId,
                question_id: answer.questionId,
                answer_id: answer.answerId,
                intensity: answer.intensity
              })
              .catch(function(err) {
                console.error(err)
              })

              return newAnswer
          })
        ).then(function (newSurveyAnswers) {
          reply(newSurveyAnswers)
        })
      }
    }
},
这是我的发帖请求:

POST /api/survey_answer HTTP/1.1
Host: 192.168.145.129:3000
Content-Type: application/json
Cache-Control: no-cache

[{"surveyResponseId":1,"questionId":1,"answerId":1,"intensity":3}]

我做错了什么?

看起来端点需要一个数组,该数组与有效负载对象中的键
responses
关联(提示:
if(request.payload.responses)

尝试将POST有效负载更改为:

{"responses": [{"surveyResponseId": 1,"questionId": 1,"answerId": 1,"intensity": 3}]}

看起来可能就是这样。今晚我试试。