该对象没问题,但是当我尝试访问某些属性时,属性具有未定义的值:
代码:
router.get('/setn', middleware.isLoggedIn, function(req, res){
var aux = false;
var arrayOfIds = req.user.following.toString();
arrayOfIds = arrayOfIds.split(",");
console.log("\n"+arrayOfIds+"\n");
User.find({_id: {$in: arrayOfIds}}, function(err, foundUsers){
if (err) {
console.log(err);
res.status(500).send("Error finding user.")
return;
}
let origLength = req.user.notifications.length;
foundUsers.forEach(function(foundUser){
foundUser.notiSchemaSent.forEach(function(notiSchema, i){
console.log("\n2\n");
if(req.user.notifications.length === 0){
req.user.notifications.unshift(notiSchema);
} else {
req.user.notifications.forEach(function(userNotification){
console.log("\n UserSchema:\n"+userNotification.username+"\n");
console.log("\n UserSchema Title:\n"+userNotification.title+"\n");
console.log("\n UserSchema Image:\n"+userNotification.image+"\n");
console.log("\n UserSchema Link:\n"+userNotification.link+"\n");
console.log("\n ====================================\n");
console.log("\n NotiSchema:\n"+notiSchema+"\n");
// OBJECT PRINT
//{ link: '5aa42e',
//image: '/Imgs/seholoko.jpg',
//title: 'Test',
//username: 'anthonyslips' }
console.log("\n NotiSchema Username:\n"+notiSchema.userName+"\n"); //Undefined
console.log("\n NotiSchema Title:\n"+notiSchema.title+"\n"); //Test
console.log("\n NotiSchema Image:\n"+notiSchema.image+"\n"); //Imgs/seholoko.jpg
console.log("\n NotiSchema link:\n"+notiSchema.link+"\n"); //Undefined
console.log("\n ====================================\n");
if(userNotification.link!== notiSchema.link){
req.user.notifications.unshift(notiSchema);
}
});
}
});
});
if (req.user.notifications.length !== origLength) {
console.log("\n5\n");
req.user.save(function(err) {
if (err) {
console.log(err);
res.status(500).send("Error saving user notifications.")
} else {
console.log("\n6\n\n");
res.json(req.user.notifications);
}
});
} else {
console.log("\n7\n\n");
res.json(req.user.notifications);
}
});
});
我在代码中添加了一些注释,其中包含打印出来的值。 我已经尝试使用JSON.stringify / parse,但我认为它不起作用,因为它已经是一个对象
我在这里缺少什么?
答案 0 :(得分:0)
不确定与image
的交易是什么,但username
的问题是您的代码为notiSchema.userName
而属性全部为小写:username: 'anthonyslips'
。