Search code examples
node.jses6-promisenode-request

Request not sending in promise


I have strung together a promise to complete three actions synchronously. First, inserting into the db and resolving the id of that insert. Second, editing my json object to contain that last inserted id from the db. And third, sending a post request using npm requests. This is all wrapped in an express endpoint.

However, the requests call doesnt seem to be posting to my API. I have checked, removed the promises dependencies (which is needed to get the last inserted id from the db), and successfully posted the data using the exact same request structure. This leads me to beleive that there is something wrong with my promise. Can anybody help?

function db() {
  return new Promise(function(resolve, reject) {
     db.run(`INSERT INTO scan_requests(name, date) VALUES(?,?);`, [req.body.name,req.body.date], function(err) {
        if (err) {
           console.log(err)
        }
        let q = this.lastID
        resolve(q)
    })
  })
}

db()
  .then(function(q) {
     let data = {
        url: 'https://api/key/',
        body: {
          name: req.body.name,
          scan_callback: `http://localhost:80/${q}` 
        },
        json: true
     }
     return(data)
}).then(function(data) {
     res.json(req.body)
     request
        .post(data)
        .on('error', function(err) {
           console.log(err)
        })
        .pipe(res)      
})

Solution

  • To chain promises, the resolve callback (executed in then) needs to return another promise, so you can chain another then to the first promise.

    Just like this:

    function db() {
                return new Promise(function (resolve, reject) {
                    const str = 'Message from first Promise'
                    resolve(str)
                })
            }
            db()
                .then(function (outputFromFirstPromise) {
                    return new Promise(function (resolve, reject) {
                        const somethingElse = ', and output from second promise'
                        const result = outputFromFirstPromise + somethingElse
                        resolve(result)
                    })
                })
                .then(function (outputFromSecondPromise) {
                    // do something with outputFromSecondPromise
                })
    

    In your case the promise in the middle is not needed at all though. Why would you use a promise, only to construct your object data if you have nothing async to handle? Just put everything in the callback of your first promise.:

    db().then(function (q) {
            let data = {
                url: 'https://api/key/',
                body: {
                    name: req.body.name,
                    scan_callback: `http://localhost:80/${q}`
                },
                json: true
            }
    
            res.json(req.body)
            request
                .post(data)
                .on('error', function (err) {
                    console.log(err)
                })
                .pipe(res)
        })