【问题标题】:how to return response to REST api in node js如何在节点 js 中返回对 REST api 的响应
【发布时间】:2018-01-30 14:19:34
【问题描述】:

我是 Node JS 的新手。我的节点 js REST api 路由代码是:

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

而我的 listByEmail 函数是:

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

我从那个 listbyemail 函数返回了这样的数据。数据在那里,如果我尝试在控制台中打印数据,它就会出现。但是在返回数据时,它不会返回。它总是返回未定义。我无法从路由中的 listByEmail 函数中捕获结果数据,也无法将其作为响应发送。请帮帮我!!!

【问题讨论】:

    标签: javascript node.js rest api express


    【解决方案1】:

    UPD一旦你了解了如何处理回调,你最好看看Promisesasync/awaitasync.js

    你的函数#findByEmail 是异步的,所以你的路由可能应该是这样的

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

    你的#listByEmail 函数应该是这样的

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

    【讨论】:

      【解决方案2】:

      在您的 ListByEmail 函数中,您正在调用异步方法 findByEmail

      当您到达return data; 行时,您的 listByEmail 函数已经返回,因此您不会向调用者返回任何内容。

      你需要异步处理,例如:

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

      然后:

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

      这是使用Promise 处理节点中的异步调用的一个非常基本的示例。你应该研究一下这是如何工作的。例如,您可以通过阅读以下内容开始:https://www.promisejs.org/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-04
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        相关资源
        最近更新 更多