简体   繁体   中英

Can't show response after post request

I'm having trouble when I try to show the answer of post request with node and request. I can see the response in the console in service, but it does not arrive into controller..why? Thaaaanks!

Here is the code:

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}

service

  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)
        } 
      }); 
    })  
  }
}

Let's focus on the following part of the code:

.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 is available in the context of the first then but not in the second! (this is the reason it's undefined ).

In order to pass it to the second then you'll need to add return response after the for-loop of the first 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)
}) 

You need to return something in every .then in the Promise if you want to use it in the next.

In this case there is not point creating a new .then and you code can be changed to:

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM