我在NodeJS方面很新,但我也很难在函数之间传递变量/对象的概念。我很感激对我做错了什么的任何帮助。
请考虑以下代码:
传入请求:
{
sender: '32165498732165845',
text: 'shopping',
originalRequest:
{
sender: { id: '32165498732165845' },
recipient: { id: '87971441647898' },
timestamp: 1488196261509,
message: { mid: 'mid.1488196261509:c7ccb7f608', seq: 36372, text: 'shopping' }
},
type: 'facebook'
}提取相关变量:
var userId = request.sender;
var listName = request.text;bot.js:
var listOps = require('./listops/test.js');
listOps.setActive(function (userId, listName, callback) {
console.log ('Here I expect a callback!');
return callback; // This will send the message back to user.
});listops/test.js:
exports.setActive = function(userId, listName, callback) {
var message = "User number " + userId + " asks to create a list with name " + listName + ".";
console.log(userId);
console.log(listName);
callback (message);
}现在我的问题是,在listOps.js中,两个控制台日志的结果不是我所期望的值,[Function]和undefined表示。因此,我怀疑这是错误消息[TypeError: callback is not a function]的根本原因。
我在兰博达使用Claudia.js。
发布于 2017-02-27 12:21:20
尝试将bot.js更改为:
var listOps = require('./listops/test.js');
listOps.setActive( userId, listName, function (message) {
console.log ('message holds the result set in listops/test.js!');
});如果您想事后处理该消息,只需将其传递给另一个函数:
bot.js
var listOps = require('./listops/test.js');
var processor = function(userId, listName, message){
... process as desired
}
listOps.setActive( userId, listName, function (message) {
console.log ('message holds the result set in listops/test.js!');
process(userId, listName, message);
});发布于 2017-02-27 12:40:35
这是因为在您的listops/test.js文件中,您定义了一个函数exports.setActive = function(userId, listName, callback),它接受三个参数userId、listName和callback,而在bot.js文件中调用该函数时,您只传递一个函数listOps.setActive(function (userId, listName, callback) {,这与setActive函数的定义所期望的那样是非法的。您需要按以下方式调用此函数
listOps.setActive(userId, listName, function() {
//your stuffs here
});https://stackoverflow.com/questions/42485445
复制相似问题