javascript - Use Express.js endpoints with promises -


i trying use express promises can't make work. when uncomment (1) can set response 200, whatever try set response in promise (2,3,4) returns 401

i can console.log data in then promise surely resolving believe 401 send because function doesn't wait promise resolved right?

what i'm doing wrong , how can set response promise?

thanks in advance!

app.get('/get_data', function (req, res) {    //res.json(200, {}); return; --> (1) gives expected 200     tree.getdata(req.param('ids'))      .then(function (data) {           // console.log('data'); --> (2) logs requested array of data          //return res.json(200,{}); --> (3) gives 401          return res.send(data); // --> (4) gives 401      }).fail(function (err) {          // never goes here          console.log(err);          return res.json(550, err);      }); }); 

you need add return before tree.getdata return promise , result. a+ promise has catch() not fail()

also when setting status code in express need use res.status().send() not res.send(status, data) express res.send() docs

app.get('/get_data', function (req, res) {    return tree.getdata(req.param('ids'))      .then(function (data) {           return res.status(200).send(data)      }).catch(function (err) {          // never goes here          console.log(err);          return res.status(550).json(err);      }); }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -