发布请求后无法显示回复

时间:2019-05-12 00:35:12

标签: javascript node.js

当我尝试用节点和请求显示发布请求的答案时遇到麻烦。我可以在使用中的控制台中看到响应,但是它没有进入控制器。为什么? Thaaaanks!

代码如下:

function postData(req, res, next){
    eventService.getItems()
    .then(response => {
        const result = response.body.items        
        const id = nameFilter.map(task => task.id =  null)

        for(var i = 16 ; i < nameFilter.length; i++){
            eventService.postData(nameFilter[i])
        } 
    })
     .then(response => {
        console.log(response) // undefined
        res.send(response)
    }) 
    .catch(error => {
        console.log('error')
        next(error)
    }) 
}

module.exports = {postData}

服务

  postData(data) {
      return new Promise(function (resolve, reject) {

      request.post({
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + '00002'
        },
        url: 'url ',
        json: data
      }, 
      function (error, response, body) {
        if (error) {
          reject(error)
        } else {
          console.log(response.body) // shows the message
          resolve(response.body)
        } 
      }); 
    })  
  }
}

2 个答案:

答案 0 :(得分:0)

让我们关注代码的以下部分:

.then(response => {
    const result = response.body.items        
    const id = nameFilter.map(task => task.id =  null)

    for(var i = 16 ; i < nameFilter.length; i++){
        eventService.postData(nameFilter[i])
    } 
})
 .then(response => { // the previous `then` didn't return anything so you shouldn't expect any argument to be passed here!
    console.log(response) // undefined
    res.send(response)
}) 

response在第一个then的上下文中可用,但在第二个不可用! (这就是undefined的原因)。

为了将其传递给第二个then,您需要在第一个return response的for循环之后添加then

.then(response => {
    const result = response.body.items        
    const id = nameFilter.map(task => task.id =  null)

    for(var i = 16 ; i < nameFilter.length; i++){
        eventService.postData(nameFilter[i])
    } 
    return response; // <-- pass it here
})
 .then(response => {
    console.log(response) // and now you'll have it!
    res.send(response)
}) 

答案 1 :(得分:0)

如果要在下一个.then中使用它,则需要在每个.then中返回它们。

在这种情况下,创建新的function postData(req, res, next){ eventService.getItems() .then(response => { const result = response.body.items const id = nameFilter.map(task => task.id = null) const promises = [] // Should 16 be here? Seems like an error for(var i = 16 ; i < nameFilter.length; i++){ promises.push(eventService.postData(nameFilter[i])) } // postData returns a promise, so above we build an array of all the promises // then this line will wait for all of them to complete return Promise.all(promises) }) .then(allResults => { // All results will now be the result of every call to `eventService.postData...` console.log(allResults) res.send(allResults) }) .catch(error => { console.log('error') next(error) }) } module.exports = {postData} 是没有意义的,您的代码可以更改为:

 final java.io.File file = new java.io.File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + str + "/Database/",  title);

   OutputStream outputStream = new FileOutputStream(file);
                    mDriveService.files().get(id).executeMediaAndDownloadTo(outputStream);