2017-12-13 4 views
1

Ich versuche, mit diesem Code zu finden und zu aktualisieren:Mongoose findByIdAndUpdate() findet/Rendite-Objekt, aber nicht aktualisiert

exports.updatePlot = async (req, res) => { 
    let modifications = {}; 
    modifications.name = req.body.name; 
    modifications.grower = req.body.props.grower; 
    modifications.variety = req.body.props.variety; 
    modifications.planted = req.body.props.planted; 

    const id = req.body.props.id; 

    try { 
     const updatedPlot = await Plot.findByIdAndUpdate(
      id, 
      { $set: { modifications } }, 
      { new: true } 
     ); 
     res.json({ 
      updatedPlot 
     }); 
    } catch (e) { 
     return res.status(422).send({ 
      error: { message: 'e', resend: true } 
     }); 
    } 
}; 

Ich kann sehen, dass meine Anfrage Körperdaten enthält, kann ich auch sehen, dass Mungo das Objekt ist zu finden, aber die Aktualisierung nicht, denn das ist, was sie als die aktualisierte Plot protokolliert:

{"_id":"id string here","name":"old name",...all other properties...} 

ich denke, meine Anfrage ungültig ist? Wer sieht meinen Fehler?

Das Schema sieht wie folgt aus:

const plotSchema = new Schema(
    { 
     name: String, 
     ...other properties... 
    }, 
    { bufferCommands: false } 
); 

const ModelClass = mongoose.model('plot', plotSchema); 

Antwort

1

Sie Ihre $set Objekt dieser Einstellung:

$set: { 
    modifications: { 
     name: 'xxx', 
     grower: 'xxx', 
     variety: 'xxx' 
    } 
} 

Aber was Sie wollen, ist:

$set: { 
    name: 'xxx', 
    grower: 'xxx', 
    variety: 'xxx' 
} 

Versuchen Sie, die geschweiften Klammern auf modifications in der Abfrage, wie das Entfernen von:

const updatedPlot = await Plot.findByIdAndUpdate(
    id, 
    { $set: modifications }, 
    { new: true } 
); 
1

denke ich, das Problem in der Anfrage ist, versuchen diese:

const updatedPlot = await Plot.findOneAndUpdate(
      {_id: id}, 
      modifications }, 
      { new: true } 
     ); 
Verwandte Themen