【问题标题】:Cast to ObjectId failed for value "" at path "_id" for model对于模型的路径“_id”处的值“”,转换为 ObjectId 失败
【发布时间】:2019-06-26 07:41:49
【问题描述】:

我已经检查了StackOverflow 上的其他条目,但没有帮助。

我正在使用node.js 构建RESTapi,并且我正在使用MongoDBmongoose 我有一个Schema,其中包含三种不同的模型。我可以将POST 请求保存到条目中。我确信该条目已保存,因为我检查了atlas.mongo。但是,当我尝试使用 GET 请求时遇到问题。

它给出了这个错误:

模型的路径“_id”处的值“”转换为 ObjectId 失败

这些是我的模型:(这些模型在不同的文件中)

const Model1 = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    word1: { type: [String], require: true }
});
----------------------------------------------
const Model2 = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    word2: { type: [String], require: true }
});
----------------------------------------------
const Model3 = mongoose.Schema({
   _id: mongoose.Schema.Types.ObjectId,
   element1: { type: [String],  default: ""},
   element2: { type: [String], default: ""}
});
----------------------------------------------
const Word = mongoose.Schema({
   _id: mongoose.Schema.Types.ObjectId,
   md3: { type: mongoose.Schema.Types.Mixed, ref: 'Model3', require: true },
   md2: { type: mongoose.Schema.Types.Mixed, ref: 'Model2', require: true },
   md1: { type: mongoose.Schema.Types.Mixed, ref: 'Model1', require: true }
});

这是我的 POST 请求:

exports.entry_create = (req, res, next) => {
const newModel3 = new Model3({
    _id: new mongoose.Types.ObjectId(),
    element1: req.body.element1,
    element2: req.body.element2
});

const newModel2 = new Model2({
    _id: new mongoose.Types.ObjectId(),
    word2: req.body.word2
});

const newModel1 = new Model1({
    _id: new mongoose.Types.ObjectId(),
    word1: req.body.word1
});

const newEntry = new Word({
    _id: new mongoose.Types.ObjectId(),
    md3: newModel3,
    md2: newModel2,
    md1: newModel1
});

newEntry
    .save(); // i have also then() and catch() part
};

这是我在Postman 上遇到错误的地方

exports.entry_get_all = (req, res, next) => {
Word.find()
    .select('_id md3 md2 md1')
    .populate('md3')
    .populate('md2')
    .populate('md1')
    .exec()
    .then(docs => {
        res.status(200).json({
            numOfEntries: docs.length,
            Entries: docs.map(doc => {
                return {
                    _id: doc._id,
                    md3: doc.md3,
                    md2: doc.md2,
                    md1: doc.md1,
                    request: { type: 'GET' }
                }
            })
        });
    }); // i have also catch() part
};

可能是什么问题? _idmd3, md2 & md1 是否返回 null

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    我相信这与您的参考 md1、md2 和 md3 有关。您引用另一个模型的方式是通过_id,在您的情况下它是和ObjectId。话虽如此,当您定义 md1、md2 和 md3 时,您会说类型是混合的,而不是 ObjectId。改为这样做:

    const Word = mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      md3: { type: mongoose.Schema.Types.ObjectId, ref: 'Model3', require: true },
      md2: { type: mongoose.Schema.Types.ObjectId, ref: 'Model2', require: true },
      md1: { type: mongoose.Schema.Types.ObjectId, ref: 'Model1', require: true }
    });
    

    另请注意:创建模型实例时,您无需显式创建新的 ObjectId。如果使用猫鼬,它会为您创建_id!所以你可以像这样创建一个新的Word

    let md1 = null;
    let md2 = null;
    let md3 = null;
    
    const newModel3 = new Model3({
      element1: req.body.element1,
      element2: req.body.element2
    });
    
    // Save newModel3
    newModel3.save()
    .then((_md3) => {
      md3 = _md3;
    
      const newModel2 = new Model2({
        word2: req.body.word2
      });
    
      return newModel2.save();
    })
    .then((_md2) => {
      md2 = _md2;
    
      const newModel1 = new Model1({
        word1: req.body.word1
      });
    
      return newModel1.save();
    })
    .then((_md1) => {
      md1 = _md1
    
      const newEntry = new Word({
        md3: md3._id,
        md2: md2._id,
        md1: md1._id
      });
    
      return newEntry.save();
    })
    

    【讨论】:

    • 我尝试了你的解决方案,当使用GET 请求时,md3, md2 & md1 返回 null
    • 啊,在保存 Word 之前,您还可以致电 newModel3.save()newModel2.save()newModel1.save() 。如果不将它们保存到数据库中,就不能使用引用
    • 啊啊啊啊,不单独保存到数据库就不能使用了?
    • 正确。我建议按顺序保存它们,链接承诺。
    猜你喜欢
    • 2021-05-12
    • 2020-05-11
    • 2023-03-28
    • 2020-10-19
    • 2017-05-23
    • 2018-07-10
    • 2020-11-19
    • 2021-07-04
    • 2017-08-18
    相关资源
    最近更新 更多