javascript - 如何通过将模式引用推送到用户的数组字段来更新用户对象

标签 javascript node.js mongodb mongoose

我的user架构有一个Notifcation对象数组。我想通过电子邮件查找用户,然后通过添加新的通知对象来更新用户的通知字段。我现在拥有的代码不会返回错误,但它也不会使用新通知更新用户的通知字段。

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

最佳答案

您可以使用 findOneAndUpdate 而不仅仅是 update 。首先,您必须找到使用您的条件,然后推送您的通知。我希望这会有所帮助

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

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

关于javascript - 如何通过将模式引用推送到用户的数组字段来更新用户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539110/

相关文章:

javascript - Graphql - 从生成的 JSON 文件创建模式

javascript - 尝试从 Node 服务器检索 json 对象时获取 "Cannot GET/"

node.js - 如何将 MongoDB 数据库与dialogfow集成

php - 上传的图片没有出现在网络上

javascript - 在 Knockoutjs 中的 ViewModel 之间共享变量状态

node.js - MERN 教程问题 : Cannot read properties of undefined (reading 'collection' )

javascript - 查询字符串模块中的 stringify 在 Jest 测试中未按预期工作

java - $set 和位置运算符在 Java 中不起作用?

javascript - 正则表达式匹配特定标记之前的所有字符

javascript - 为什么不能在vue模板中使用window?