【问题标题】:Mongoose Nested Document returning undefined猫鼬嵌套文档返回未定义
【发布时间】:2015-01-06 13:45:57
【问题描述】:

我对 Mongoose 有这样的问题:

MongoDB 中的对象

{
  _id:1928319237192387,
  componists:
    {
       name:'George Enescu'
    }
}

猫鼬查询

var find = function(req, res, next) {

return IP.findById(req.params.id, function(err, artist) {

  if(!artist) {
    res.status(404).send({
      status: 'Not Found'
    });
  } else if (!err) {
    console.log(JSON.stringify(artist.componists.name));
  }
}

输出:

undefined

预期输出:

George Enescu

但是记录 artist.componists 有效,回馈 {name:'George Enescu'}

【问题讨论】:

    标签: node.js express mongoose


    【解决方案1】:

    已经发现错误。我必须确保在艺术家模式中定义正确的类型。

    var ArtistSchema = mongoose.Schema({
    
        componists: {type:String}
    });
    

    我最初将类型设置为type:Number,返回undefined

    虚拟 + 无模式 = 错误?

    但是,我还注意到,如果我将架构定义为 Mixed {},那么虚拟将无法工作。

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多