如何通过将架构ref推送到用户的数组字段来更新User对象

时间:2016-04-11 02:57:44

标签: javascript node.js mongodb mongoose

我的user架构有一组Notifcation个对象。我希望通过他们的电子邮件找到用户,然后通过添加新的user's对象来更新notification通知字段。我现在的代码现在没有返回错误,但它也没有通过新通知更新user's通知字段。

        var notification = { type: data.notification_type, from: socket.request.user._id };
        notification = new Notification(notification);

        User.update({ email: data.to }, { $push: { notifications: notification } }, function(err, model) {
            if (err) console.log(err);
        }); 

用户架构

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: 'Notifcation'
    }]
});

0 个答案:

没有答案