2016-08-23 1 views
0

У меня есть схема обмена, которая будет содержать двух пользователей. Я хочу, чтобы сообщения были удалены от одного пользователя, чтобы другой получатель все еще мог прочитать сообщение.

Схема

// Messages Schema 
var messagesSchema = new Schema({ 
    from: { 
     type: Schema.Types.ObjectId, 
     required: true, 
     ref: 'User', 
    }, 
    content: { 
     type: String, 
     required: true, 
     trim: true 
    }, 

    deleted_by: [{ 
     type: Schema.Types.ObjectId, 
     ref: 'User' 
    }], 

    read_by: [{ 
     type: Schema.Types.ObjectId, 
     ref: 'User' 
    }], 

}, { 
    timestamps: true 
}); 

// Conversation Schema 
var conversationsSchema = new Schema({ 

    recipients: [{ 
     type: Schema.Types.ObjectId, 
     required: true, 
     ref: 'User', 
     index: true 
    }], 

    messages: [messagesSchema], 

}, { 
    timestamps: true 
}); 

Теперь, когда я создаю разговор между двумя пользователями, она будет выглядеть следующим

[ 
    { 
    "_id": "57bb6fed3d001f054e809175", 
    "updatedAt": "2016-08-22T21:37:30.631Z", 
    "createdAt": "2016-08-22T21:34:37.381Z", 
    "__v": 2, 
    "messages": [ 
     { 
     "updatedAt": "2016-08-22T21:34:37.380Z", 
     "createdAt": "2016-08-22T21:34:37.380Z", 
     "from": "57b7448668d04d3035774b9a", 
     "content": "Hello are you there?", 
     "_id": "57bb6fed3d001f054e809176", 
     "read_by": [], 
     "deleted_by": [] 
     }, 
     { 
     "updatedAt": "2016-08-22T21:34:58.060Z", 
     "createdAt": "2016-08-22T21:34:58.060Z", 
     "from": "57b7448668d04d3035774b9a", 
     "content": "I miss you", 
     "_id": "57bb70023d001f054e809177", 
     "read_by": [], 
     "deleted_by": [] 
     }, 
     { 
     "updatedAt": "2016-08-22T21:37:30.631Z", 
     "createdAt": "2016-08-22T21:37:30.631Z", 
     "from": "57b7816b68d04d3035774b9b", 
     "content": "Hey... Me too", 
     "_id": "57bb709a3d001f054e809178", 
     "read_by": [], 
     "deleted_by": [] 
     } 
    ], 
    "recipients": [ 
     "57b7448668d04d3035774b9a", 
     "57b7816b68d04d3035774b9b" 
    ] 
    } 
] 

Теперь, когда один из пользователей хотят, чтобы удалить беседу с его стороны Я хочу добавить идентификатор пользователя в массив deleted_by внутри каждого сообщения.

Я пытаюсь сделать что-то вроде этого

Conversation.findOneAndUpdate({ 
    _id: conversation_id 
}, { 
    $push: { 
     'messages.deleted_by': req.loggedInUser._id 
    } 
}, function(err, data) { 
    if(err) return next(err); 
    res.json(data); 
}) 

Ошибка Возвращается

TypeError: Cannot read property '$isMongooseDocumentArray' of undefined 

Я попытался добавить $ знак и все еще получаю ту же ошибку.

ответ

0

Попробуйте это:

Conversation.findOne({ 
    _id: conversation_id 
}, function(err, docs) { 
    if(err) return next(err); 
    if(docs) 
    { 
     docs.messages.forEach(function(msg,index,array) 
     { 
      msg.deleted_by.push(req.loggedInUser._id); 
     }); 
     docs.save(); 
    } 
}); 

Read this для лучшего понимания функции Foreach.

Надеюсь, это поможет.

Смежные вопросы