Nodejs:在异步结果上返回结果 [英] Nodejs: returning result on async result

查看:50
本文介绍了Nodejs:在异步结果上返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 nodejs 中编写一个 RESTfull API,它基本上围绕一个控制器/模型架构,我遇到了一些关于 nodejs 异步性质的问题:

I'm trying to code an RESTfull API in nodejs which is basically around a controller/modele schema and I meet some problems about the async nature of nodejs:

Station.js:(控制器)

Station.js: (controller)

'use strict';

var url = require('url');

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

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

    var result = Stations.stationsGet(req.swagger.params['arg']);

    if(typeof result !== 'undefined') {
        res.setHeader('Content-Type', 'application/json');
        res.end(JSON.stringify(result || {}, null, 2));
    }
    else
        res.end();
};

StationService.js:(模型)

StationService.js: (modele)

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

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

    db.execute(sql, {foo: param}, db.queryResult.any, function(result){
        // 'result' containing the query data
    });

    // Ideally: data_output = result;

    return data_output;
}

问题是如果我在 db.execute 上使用回调继续,我必须提供所有控制器上下文(res,...)来回复客户端,并且它破坏了模型/控制器架构,因为我modele 负责剩下的控制器工作.

The problem is if I use callback on my db.execute to continue, I have to give all the controller context (res, ...) to reply back to the client, and it break the modele/controller schema since my modele does the remaining controller work.

有没有一种(简单的?)方法可以在stationGet() 中获取查询结果然后返回它?这是否真的违反了 nodejs 的性质,如果是这样,在这种情况下如何采取正确的行为?

Is there a (easy?) way to get the result of the query in stationsGet() and then returning it? Is it really against the nodejs nature and if so how to adopt the correct behavior in that case?

PS:我正在使用 swagger,它为 nodejs 生成了文件和基本结构.

PS: I'm using swagger which has generated the files and base structure for nodejs.

推荐答案

在这种情况下你应该使用回调(也看看 promises)

You should use a callback in this case (take a look at promises as well)

您的控制器将如下所示:

your controller will look like this:

'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,如下所示:

And the model you made must accept a callback as a last parameter, and you return err and the result like follows:

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

希望对你有帮助

这篇关于Nodejs:在异步结果上返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆