您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Node.js:在异步结果上返回结果

Node.js:在异步结果上返回结果

在这种情况下,您应该使用回调(也请看一下promise)

您的控制器将如下所示:

'use strict';

var url = require('url');

var Stations = require('./StationsService');

module.exports.stationsGet = function stationsGet(req, res, next){

    Stations.stationsGet(req.swagger.params['arg'], function(err, result) {
        if(typeof result !== 'undefined') {
            res.setHeader('Content-Type', 'application/json');
            res.end(JSON.stringify(result || {}, null, 2));
        }
        else
            res.end();
    });
};

并且您创建的模型必须接受回调作为最后一个参数,然后返回errresult如下所示:

'use strict';
exports.stationsGet = function(param, cb){
    var data_output = {};

    var sql = 'SELECT * FROM foo WHERE args = ${foo}';

    db.execute(sql, {foo: param}, db.queryResult.any, function(result){
       cb(null, result); // first parameter is the error and the second is the result, this is pretty standard in node
    });
}

我希望这可以帮助你

Node 2022/1/1 18:49:03 有492人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶