2012-10-06 42 views
5

Tôi đang cố gắng cập nhật một số nội dung trong Mongoose.js 3.1.2 và tôi không thể có được hai chức năng này để hoạt động. Bất kỳ ý tưởng tại sao? Cảm ơn ...khắc phục sự cố trong Mongoose 3

function(req, res) { 
    Content.findById(req.body.content_id, function(err, content) { 
    // add snippet to content.snippets 
    content.snippets[req.body.snippet_name] = req.body.snippet_value; 
     content.save(function(err) { 
     res.json(err || content.snippets); 
    }); 
    } 
} 


function(req, res) { 
    Content.findById(req.body.content_id, function(err, content) { 

     // delete snippets 
     delete content.snippets[req.body.snippet_name]; 
     //content.snippets[req.body.snippet_name] = undefined; <-- doesn't work either 

     content.save(function(err) { 
     res.json(err || "SUCCESS"); 
     }); 

    }); 
} 

schema của tôi trông giống như sau:

contentSchema = new Schema(
    title: String, 
    slug: String, 
    body: String, 
    snippets: Object 
); 

Trả lời

10

Bạn có thể cần phải đánh dấu những con đường như sửa đổi. Mongoose có thể không kiểm tra các thuộc tính đối tượng vì bạn không tạo một lược đồ nhúng cho chúng.

function(req, res) { 
    Content.findById(req.body.content_id, function(err, content) { 
    // add snippet to content.snippets 
    content.snippets[req.body.snippet_name] = req.body.snippet_value; 
    content.markModified('snippets'); // make sure that Mongoose saves the field 
     content.save(function(err) { 
     res.json(err || content.snippets); 
    }); 
    } 
} 
+0

Oh wow, bạn hoàn toàn thổi tâm trí của tôi, cảm ơn vì điều này: D – red

+0

Có! Làm thế nào tôi đã bỏ lỡ nó. Cảm ơn Bill – Pardoner

+0

Bạn là anh hùng của tôi. – ehaydenr

Các vấn đề liên quan