app.get('/showme', function (req, res){
console.log(req.session.user);
UserModel.findOne({ user: req.session.user }, function (err, user){
if (err) throw err;
console.log(user);
res.send('Look');
});
});
执行此操作时,我在终端中正确收到了用户,但是之后,应用程序突然崩溃了,这就是错误TypeError: Cannot read property '_id' of null
。已req.session.user
定义,并且在应用程序中。这不是第一次UserModel.findOne({ user: req.session.user }
调用,而是唯一返回此错误的调用!为什么会这样?
编辑:
这是用户的模式
var userschema = new mongoose.Schema({
user: String,
pass: String,
mail: String,
avatar: String,
});
编辑:
我不知道为什么,但我没有收到任何错误UserModel.find()
。它只发生在UserModel.findOne()
.
编辑:
app.get('/showme', function (req, res){
UserModel.find({ user: req.session.user }, function (err, user){
for (var i = 0; i <= user[0].following.length; i++) {
UserModel.find({ _id: user[0].following[i] }, function (err, users){
var userss = [];
users[i] = users[i];
console.log(users);
});
}
});
res.send('Hello');
});
在这段代码中,第一次UserModel
不会返回任何错误,但第二次会!这是我在终端收到的:
[ { __v: 2,
_id: 50e1d852d7b6f9e1be000003,
avatar: './users/rodridev/full_Mgs-solid-snake.jpg',
user: 'rodridev',
notification: [],
followers: [ '50e1cfda6e63107eb6000003' ],
following: [ '50e1cfda6e63107eb6000003' ],
,
,
undefined ]
[ , , , undefined ]
[ { __v: 1,
_id: 50e214171553600000000003,
avatar: './users/kirbo/8816.masterchief.jpg-610x0.jpg',
user: 'kirbo',
followers: [ '50e1cfda6e63107eb6000003' ],
following: [],
imagen: [] },
,
,
undefined ]
这就是出现的错误。和之前一样:TypeError: Cannot read property '_id' of null