[英]How to return from an inner function (SuperAgent) in nodeJs
'use strict';
const express = require('express');
const app = express();
var request = require('superagent');
var list_profs_ues = [
{
"id_prof": 0,
"id_ue": 2
},
{
"id_prof": 1,
"id_ue": 4
},
{
"id_prof": 3,
"id_ue": 0
},
{
"id_prof": 4,
"id_ue": 1
},
{
"id_prof": 2,
"id_ue": 3
}
];
app.get('/service/:name_prof', (req, callback) => {
request.get(`http://localhost:5020/service/${req.params.name_prof}`, function(err, res){
var result_prof_id = res.body.text;
console.log(result_prof_id);
var result_ue_id;
for (var i = 0; i < list_profs_ues.length; i++){
if (list_profs_ues[i]["id_prof"] == result_prof_id){
console.log(list_profs_ues[i]["id_ue"]);
result_ue_id = list_profs_ues[i]["id_ue"];
}}
request.get(`http://localhost:5030/service/${result_ue_id}`, function(err, res){
if(err || res.body.text == ""){
return callback("some result");
}else{
return callback("some result");
}
});
});
});
module.exports = app;
const server = app.listen(process.env.PORT || 5040, () => {
console.log('Express server listening on port %d in %s mode',
server.address().port, app.settings.env);
});
This, displays an error saying : callback is not a function.... My question is quite simple, how can return that "some result" ?? 这会显示一条错误消息:回调不是函数。...我的问题很简单,如何返回“某些结果”? I've tried : return "some result";
我试过了:返回“一些结果”;
Any suggestion will be more than apperciated! 任何建议都将不胜感激!
The second parameter of the middleware function in app.get
is not a callback, but the response object. app.get
中的中间件函数的第二个参数不是回调,而是响应对象。 The callback is the third parameter: 回调是第三个参数:
app.get('/service/:name_prof', (req, res, next) => { ...
http://expressjs.com/en/guide/writing-middleware.html http://expressjs.com/en/guide/writing-middleware.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.