簡體   English   中英

貓鼬,填充字段返回一個空數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM