我使用Node.js和mongoose爲應用程序創建後端,用戶可以在其中互相通信。無法調用未定義的方法推送(定義時)
架構
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = mongoose.Schema({
name : String,
nick: String,
reg_id: String,
friends: {
type: Array,
'default': []
}
});
mongoose.connect('mongodb://localhost:27017/DB');
module.exports = mongoose.model('users', userSchema);
請求
exports.addfriend = function(reg_id,friend,callback) {
user.find({reg_id:reg_id},function(err,guy){
// found the user, now add his friend
user.find({name:friend}, function(err,friendFound){
console.log("trying to add friend to:"+guy)
console.log("his current friends:"+guy.friends)
guy.friends.push(friendFound.reg_id)
callback({'response':guy.name+' is now friends with '+friendFound.name});
})
});
}
控制檯爲什麼限定
trying to add friend to:{ _id: 5483d2c76dd64ee412bfe865,
name: 'Hello',
nick: 'hey',
reg_id: 'XXXXXXXXX',
__v: 0,
friends: [] }
his current friends:undefined
C:\..\config\requests.js:82
guy.friends.push(friendFound.reg_id)
TypeError: Cannot call method 'push' of undefined
表示了陣列當它在模式中定義並且我正在回顧正確的用戶(傢伙)時?感謝您的幫助
對象'friendFound'是數據還是null? – 2014-12-07 04:27:09