[英]Mongoose, populating field returns an empty array
我知道我的user
notifications
字段中包含對象,但是當我呼叫填充notifications
它們正在消失。
User.findById(socket.request.user._id).populate('notifications').exec(function(err, user) {
console.log('get notification populated user', user);
var unread = user.unread;
var notifications = user.notifications;
socket.emit('take notifications', {
notifications: notifications,
num_unread: unread
});
});
var UserSchema = new Schema({
firstName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
organization: {
type: String,
trim: true,
default: '',
required: 'Please fill in an organization name'
},
position: {
type: String,
trim: true,
default: '',
required: 'Please fill in the title of your position'
},
displayName: {
type: String,
trim: true
},
email: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your email'],
match: [/.+\@.+\..+/, 'Please fill a valid email address']
},
username: {
type: String,
unique: 'testing error message',
required: 'Please fill in a username',
trim: true
},
password: {
type: String,
default: '',
validate: [validateLocalStrategyPassword, 'Password should be longer']
},
salt: {
type: String
},
provider: {
type: String,
required: 'Provider is required'
},
providerData: {},
additionalProvidersData: {},
roles: {
type: [{
type: String,
enum: ['user', 'admin']
}],
default: ['user']
},
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
/* For reset password */
resetPasswordToken: {
type: String
},
resetPasswordExpires: {
type: Date
},
notifications: [{
type: Schema.ObjectId,
ref: 'Notification'
}],
unread: {
type: Number,
default: 0
}
});
問題是我在創建Notifcations
時保存了這些Notifcations
。 所以我改變了這個
notification = new Notification(notification);
console.log('pushing notification', notification);
User.findOneAndUpdate({ email: data.to }, {
$push: { notifications: notification },
$inc: { unread: 1 }
},
function(err, model) {
if (err) console.log('Error updating user with new notification', err);
else {
// console.log('sending notification to', toSocket.id.substring(toSocket.id.indexOf('#') + 1));
toSocket.emit('new notification', { notification: data.notification_type });
}
});
對此
notification = new Notification(notification);
notification.save(function(err) {
console.log('something went wrong saving the notification');
});
console.log('pushing notification', notification);
User.findOneAndUpdate({ email: data.to }, {
$push: { notifications: notification },
$inc: { unread: 1 }
},
function(err, model) {
if (err) console.log('Error updating user with new notification', err);
else {
// console.log('sending notification to', toSocket.id.substring(toSocket.id.indexOf('#') + 1));
toSocket.emit('new notification', { notification: data.notification_type });
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.