Request not sending in promise












1















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









share|improve this question























  • Did you put a breakpoit after let q = this.lastID and checked, that it contains any value? My guess is that this.lastID is not defined. However, by setting breakpoints in your code, you should be able to debug the problem. Also your first call to then does not return a promise itself and by this cannot be chained with another then.

    – Fuzzzzel
    Nov 13 '18 at 19:38













  • @Fuzzzzel I am console logging data after the second .then and ${q} contains what it should, so it is not undefined. How would you about chaining together the request call then?

    – Hysii
    Nov 13 '18 at 19:52











  • Did my answer help you in any way or is anything still unclear?

    – Fuzzzzel
    Nov 18 '18 at 14:09
















1















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









share|improve this question























  • Did you put a breakpoit after let q = this.lastID and checked, that it contains any value? My guess is that this.lastID is not defined. However, by setting breakpoints in your code, you should be able to debug the problem. Also your first call to then does not return a promise itself and by this cannot be chained with another then.

    – Fuzzzzel
    Nov 13 '18 at 19:38













  • @Fuzzzzel I am console logging data after the second .then and ${q} contains what it should, so it is not undefined. How would you about chaining together the request call then?

    – Hysii
    Nov 13 '18 at 19:52











  • Did my answer help you in any way or is anything still unclear?

    – Fuzzzzel
    Nov 18 '18 at 14:09














1












1








1








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









share|improve this question














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






node.js es6-promise node-request






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 19:30









HysiiHysii

707




707













  • Did you put a breakpoit after let q = this.lastID and checked, that it contains any value? My guess is that this.lastID is not defined. However, by setting breakpoints in your code, you should be able to debug the problem. Also your first call to then does not return a promise itself and by this cannot be chained with another then.

    – Fuzzzzel
    Nov 13 '18 at 19:38













  • @Fuzzzzel I am console logging data after the second .then and ${q} contains what it should, so it is not undefined. How would you about chaining together the request call then?

    – Hysii
    Nov 13 '18 at 19:52











  • Did my answer help you in any way or is anything still unclear?

    – Fuzzzzel
    Nov 18 '18 at 14:09



















  • Did you put a breakpoit after let q = this.lastID and checked, that it contains any value? My guess is that this.lastID is not defined. However, by setting breakpoints in your code, you should be able to debug the problem. Also your first call to then does not return a promise itself and by this cannot be chained with another then.

    – Fuzzzzel
    Nov 13 '18 at 19:38













  • @Fuzzzzel I am console logging data after the second .then and ${q} contains what it should, so it is not undefined. How would you about chaining together the request call then?

    – Hysii
    Nov 13 '18 at 19:52











  • Did my answer help you in any way or is anything still unclear?

    – Fuzzzzel
    Nov 18 '18 at 14:09

















Did you put a breakpoit after let q = this.lastID and checked, that it contains any value? My guess is that this.lastID is not defined. However, by setting breakpoints in your code, you should be able to debug the problem. Also your first call to then does not return a promise itself and by this cannot be chained with another then.

– Fuzzzzel
Nov 13 '18 at 19:38







Did you put a breakpoit after let q = this.lastID and checked, that it contains any value? My guess is that this.lastID is not defined. However, by setting breakpoints in your code, you should be able to debug the problem. Also your first call to then does not return a promise itself and by this cannot be chained with another then.

– Fuzzzzel
Nov 13 '18 at 19:38















@Fuzzzzel I am console logging data after the second .then and ${q} contains what it should, so it is not undefined. How would you about chaining together the request call then?

– Hysii
Nov 13 '18 at 19:52





@Fuzzzzel I am console logging data after the second .then and ${q} contains what it should, so it is not undefined. How would you about chaining together the request call then?

– Hysii
Nov 13 '18 at 19:52













Did my answer help you in any way or is anything still unclear?

– Fuzzzzel
Nov 18 '18 at 14:09





Did my answer help you in any way or is anything still unclear?

– Fuzzzzel
Nov 18 '18 at 14:09












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288237%2frequest-not-sending-in-promise%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














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





    share|improve this answer




























      1














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





      share|improve this answer


























        1












        1








        1







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





        share|improve this answer













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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 20:43









        FuzzzzelFuzzzzel

        1,39921430




        1,39921430






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288237%2frequest-not-sending-in-promise%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings