Javascript 回调并返回Node.js
我在这里遇到了一个问题,我想单独保存我的函数(在文件中),以便代码更整洁 在my Route.js中,我调用的函数如下:Javascript 回调并返回Node.js,javascript,node.js,mongoose,callback,Javascript,Node.js,Mongoose,Callback,我在这里遇到了一个问题,我想单独保存我的函数(在文件中),以便代码更整洁 在my Route.js中,我调用的函数如下: app.post('/pay', function(req, res){ User.getPaypal(function(output){ console.log(output) //i am not getting this result(output) }) }) 该函数在另一个文件中导出,如下所示: module.exports.ge
app.post('/pay', function(req, res){
User.getPaypal(function(output){
console.log(output) //i am not getting this result(output)
})
})
该函数在另一个文件中导出,如下所示:
module.exports.getPaypal = function(){
var create_payment_json= {
//some values
};
paypal.payment.create(create_payment_json, function (err, payment) {
if (err) {
return err;
} else {
return payment;
}
});
}
我希望获得payment或err的返回值,作为路由中被调用函数的返回值
<>我怎样才能完成这项工作?让我们后退一步,考虑函数如何工作的基本知识。 假设您编写了函数:
function double () {
var x = 1;
return x * 2;
}
var getPaypal = function () {
/** for now it does not matter what's here **/
}
那你就称它为
var y = double(100);
你看,y
是2而不是200
你认为这有什么不对
如果你说你已经声明不接受论点,你是对的。解决办法是:
function double (x) {
return x * 2;
}
现在让我们看看您的函数:
function double () {
var x = 1;
return x * 2;
}
var getPaypal = function () {
/** for now it does not matter what's here **/
}
现在,您调用函数的方式是:
function mycallback (output) {
console.log(output);
}
getPaypal(mycallback);
function getPaypal() {}
我希望你知道怎么了。很明显,您已将函数声明为:
function mycallback (output) {
console.log(output);
}
getPaypal(mycallback);
function getPaypal() {}
当您想要的是:
function getPaypal(anotherFunction) {}
现在,如何将结果传递给回调函数?简单,就叫它:
function getPaypal(anotherFunction) {
/** some processing **/
anotherFunction(result); // this is how you pass the result to the callback
}
回调与数字、字符串或数组没有区别。它只是传递给你的函数的一部分。让我们后退一步,考虑函数如何工作的基本知识。 假设您编写了函数:
function double () {
var x = 1;
return x * 2;
}
var getPaypal = function () {
/** for now it does not matter what's here **/
}
那你就称它为
var y = double(100);
你看,y
是2而不是200
你认为这有什么不对
如果你说你已经声明不接受论点,你是对的。解决办法是:
function double (x) {
return x * 2;
}
现在让我们看看您的函数:
function double () {
var x = 1;
return x * 2;
}
var getPaypal = function () {
/** for now it does not matter what's here **/
}
现在,您调用函数的方式是:
function mycallback (output) {
console.log(output);
}
getPaypal(mycallback);
function getPaypal() {}
我希望你知道怎么了。很明显,您已将函数声明为:
function mycallback (output) {
console.log(output);
}
getPaypal(mycallback);
function getPaypal() {}
当您想要的是:
function getPaypal(anotherFunction) {}
现在,如何将结果传递给回调函数?简单,就叫它:
function getPaypal(anotherFunction) {
/** some processing **/
anotherFunction(result); // this is how you pass the result to the callback
}
回调与数字、字符串或数组没有区别。它只是传递给你的函数的东西。你应该从理解基于 至于您的问题,您缺少使用传递的回调函数。它应该如下所示
module.exports.getPaypal = function(callback){ //callback argument was missing
var create_payment_json= {
//some values
};
paypal.payment.create(create_payment_json, function (err, payment) {
if (err) {
callback(undefined, err); // callback function being invoked
} else {
callback(payment, undefined); // would be better if you have two arguments to callback first for result second for error
}
});
}
你们应该从理解这个概念开始,这个概念是建立在概念的基础上的 至于您的问题,您缺少使用传递的回调函数。它应该如下所示
module.exports.getPaypal = function(callback){ //callback argument was missing
var create_payment_json= {
//some values
};
paypal.payment.create(create_payment_json, function (err, payment) {
if (err) {
callback(undefined, err); // callback function being invoked
} else {
callback(payment, undefined); // would be better if you have two arguments to callback first for result second for error
}
});
}
您在
console.log(output)
中得到了什么?我实际上想要得到支付的返回值。您在console.log(output)
中得到了什么?我实际上想要得到支付的返回值。