2012-10-06 2 views
5

Mongoose.js 3.1.2에서 일부 내용을 업데이트하려고하는데이 두 함수가 작동하지 않습니다. 어떤 아이디어? 덕분에 ...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"); 
     }); 

    }); 
} 

내 스키마는 다음과 같은 :

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

답변

10

당신은 수정 된 경로를 표시해야 할 수 있습니다. 몽구스는 당신이 임베디드 스키마를 만들지 않았기 때문에 객체 속성을 검사하지 않을 수도 있습니다.

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

오, 와우, 당신은 완전히 내 마음을 불어 넣어 주셔서 감사합니다. D – red

+0

예! 나는 그것을 어떻게 놓쳤는가? 감사 빌 – Pardoner

+0

당신은 내 영웅입니다. – ehaydenr

관련 문제