javascript - how to return response to REST api in node js - Stack Overflow

I'm new to Node JS. My node js REST api route code is:'use strict';module.exports = fun

I'm new to Node JS. My node js REST api route code is:

'use strict';
module.exports = function(app) {
    var sequel = require('../controllers/sampleController');
    app.get('/task?:email', function(req, res){
        res.send(sequel.listByEmail(req.query.email));
    });
};

And my listByEmail function is:

'use strict';
var apiKey = '1xxxxxxxxL';
exports.listByEmail = function(emailid) {
    console.log(emailid);
    if(emailid != null && emailid != undefined) {
        var xyz = require("xyz-api")(apiKey);
        xyz.person.findByEmail(emailid, function(err, data) {
            if(data.status == 200){
                return data; // data is in json format
            }
        });
    }
};

I returned data like this from that listbyemail function. Data is there, if i try to print the data in console it appears. But while returning the data, it won't returned. It's always return undefined. I can't able to catch the result data from listByEmail function in route and not able to send it as response. Please helpMe!!!

I'm new to Node JS. My node js REST api route code is:

'use strict';
module.exports = function(app) {
    var sequel = require('../controllers/sampleController');
    app.get('/task?:email', function(req, res){
        res.send(sequel.listByEmail(req.query.email));
    });
};

And my listByEmail function is:

'use strict';
var apiKey = '1xxxxxxxxL';
exports.listByEmail = function(emailid) {
    console.log(emailid);
    if(emailid != null && emailid != undefined) {
        var xyz = require("xyz-api")(apiKey);
        xyz.person.findByEmail(emailid, function(err, data) {
            if(data.status == 200){
                return data; // data is in json format
            }
        });
    }
};

I returned data like this from that listbyemail function. Data is there, if i try to print the data in console it appears. But while returning the data, it won't returned. It's always return undefined. I can't able to catch the result data from listByEmail function in route and not able to send it as response. Please helpMe!!!

Share Improve this question asked Aug 22, 2017 at 10:30 Saravanan CMSaravanan CM 391 gold badge5 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

In your ListByEmail function you are calling an asynchronous method, findByEmail.

When you reach the return data; line, your listByEmail function already returned so you are not returning anything to the caller.

You need to handle it asynchronously, for example:

'use strict';
var apiKey = '1xxxxxxxxL';
exports.listByEmail = function(emailid) {
    return new Promise(function(resolve, reject) {
        console.log(emailid);
        if(emailid != null && emailid != undefined) {
            var xyz = require("xyz-api")(apiKey);
            xyz.person.findByEmail(emailid, function(err, data) {
                if(data.status == 200){
                    resolve(data); // data is in json format
                }
            });
        } else {
            reject("Invalid input");
        }
    };

Then:

'use strict';
module.exports = function(app) {
    var sequel = require('../controllers/sampleController');
    app.get('/task?:email', function(req, res){
        sequel.listByEmail(req.query.email).then(function(data) {
            res.send(data);
        });
    });
};

This is a very basic example of using Promise to handle asynchronous calls in node. You should study a little bit how this works. You can start for example by reading this: https://www.promisejs/

UPD Once you understand how to deal with callbacks, you'd better to look towards Promises, async/await, and async.js

Your function #findByEmail is asynchronous, so possibly your route should look like

'use strict';
module.exports = function(app) {
    var sequel = require('../controllers/sampleController');
    app.get('/task?:email', function(req, res){
        sequel.listByEmail(req.query.email, function(err, list){
          if(err){
            console.error(err);
            //handle error
          }
          res.send(list);
        })
    });
};

and your #listByEmail function should be like

'use strict';
var apiKey = '1xxxxxxxxL';
exports.listByEmail = function(emailid, callback) {
    console.log(emailid);
    if(emailid != null && emailid != undefined) {
        var xyz = require("xyz-api")(apiKey);
        xyz.person.findByEmail(emailid, function(err, data) {
            if(err){
              callback(err);
            } else if(data.status == 200){
                callback(null, data);
            }
        });
    }
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744304512a4567663.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信