2012-10-06 12 views
5

Próbuję zaktualizować niektóre treści w Mongoose.js 3.1.2 i nie mogę uzyskać tych dwóch funkcji do pracy. Jakieś pomysły, dlaczego? Dzięki ...Zapisywanie problemów w 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"); 
     }); 

    }); 
} 

Mój schemat wygląda mniej więcej tak:

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

Odpowiedz

10

może trzeba zaznaczyć ścieżki jako zmodyfikowane. Mongoose może nie sprawdzać właściwości obiektu, ponieważ nie utworzył dla nich osadzonego schematu.

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

Och, wow, całkowicie mnie roztrzęsiłeś, dzięki za to: D – red

+0

Tak! Jak to przegapiłem. Dzięki, Bill – Pardoner

+0

Jesteś moim bohaterem. – ehaydenr

Powiązane problemy