我正在尝试在NodeJS中创建多个用户对象并将其存储在地图中。这些将由我正在开发的聊天机器人使用。
基本上应该发生的是这样:
我的用户代码如下:
'use strict';
const self = module.exports = {
loadedUsers: new Map(),
add: function(senderID, data) {
// Code to load an existing user from the database
loadedUsers.set(senderID, new User(data.userID, data.gender, data.age));
},
addNewUser: function(senderID) {
// Code to create a blank user object in order to start creating user profile
loadedUsers.set(senderID, new User());
},
User: function(userID, gender, age) {
if (typeof userID === 'undefined' && typeof gender === 'undefined'
&& typeof age === 'undefined') {
this.userID = userID;
this.gender = gender;
this.age = age;
} else {
this.userID = undefined;
this.gender = undefined;
this.age = undefined;
}
},
};
当我要创建一个全新的空白用户时,只需调用addNewUser(senderID)
函数。理论上,这应该将一个空白用户添加到loadedUsers()映射中。但事实并非如此。相反,我收到此错误:
Nov 09 15:16:20 weightmentor app/web.1: /app/app_modules/handler.js:255
Nov 09 15:16:20 weightmentor app/web.1: user.addNewUser(senderID);
Nov 09 15:16:20 weightmentor app/web.1: ^
Nov 09 15:16:20 weightmentor app/web.1: TypeError: Cannot read property 'addNewUser'
of undefined
Nov 09 15:16:20 weightmentor app/web.1: at Timeout._onTimeout
(/app/app_modules/handler.js:255:34)
Nov 09 15:16:20 weightmentor app/web.1: at ontimeout (timers.js:436:11)
Nov 09 15:16:20 weightmentor app/web.1: at tryOnTimeout
(timers.js:300:5)
Nov 09 15:16:20 weightmentor app/web.1: at listOnTimeout
(timers.js:263:5)
Nov 09 15:16:20 weightmentor app/web.1: at Timer.processTimers
(timers.js:223:10)
我不确定为什么会发生此错误。尚不确定,但问题是什么?
该函数的调用方式如下:
common.getFBUser(senderID, function(name) {
greetingText = strings.initialGreeting.replace("%USER", name);
delay = common.setDelay(greetingText);
messaging.sendTextMessage(senderID, greetingText);
setTimeout(function() {
// Pause before starting user profile creation
user.addNewUser(senderID);
user.setLoadedUserFirstName(senderID, name);
dialogflow.sendRequest(session.getIDs(), senderID, strings.createUser);
}, delay);
});
请注意,common.getFBUser
只是使用Graph.API从Facebook个人资料中获取用户的名字。
答案 0 :(得分:0)
我知道了。调用addNewUser(senderID)
函数的代码部分如下所示:
getUser(senderID, function(result) {
if (common.isDefined(result)) {
// If a user has been returned from the database, add to the usermap.
user.add(senderID, result);
// Once user is added, welcome the user
greetUser(senderID);
} else {
// User has not been returned from the database, this is a new user, so create a user profile
common.getFBUser(senderID, function(name) {
greetingText = strings.initialGreeting.replace("%USER", name);
delay = common.setDelay(greetingText);
messaging.sendTextMessage(senderID, greetingText);
setTimeout(function() {
// Pause before starting user profile creation
user.addNewUser(senderID, name);
dialogflow.sendRequest(session.getIDs(), senderID, strings.createUser);
}, delay);
});
}
});
此代码块的第1行最初读取为getUser(senderID, function(user) {
这意味着当我在第15行调用user.addNewUser(senderID, name)
而不是在用户模块中调用addNewUser()
函数时,代码试图在{{1 }}是在第1行的回调函数中传递的。这当然不存在。当我将第1行更改为读取addNewUser(senderID, name)
时,效果很好。