javascript - Expressjs Mongoose 查找嵌套嵌入文档未定义

标签 javascript node.js mongodb express mongoose

我正在使用 nodejs 构建游戏0.6.18 expressjs 2.5.8 和 mongoose 2.6.7.

我正在尝试使用 Mongoose 存储和检索嵌入式文档。 考虑这个例子:

用户架构

var User = module.exports = new Schema({
      username: { type: String }
    , characters: [Character]
    , created: { type: Date, default: Date.now }
});

字符模式

var Character = module.exports = new Schema({
      name: { type: String }
    , spells: [Spell]
    , created: { type: Date, default: Date.now }
});

拼写架构

var Spell = module.exports = new Schema({
      name: { type: String }
    , created { type: Date, default: Date.now }
});

Mongoose

var db = mongoose.createConnection('mongodb://localhost/mygame');
mongoose.model('User', require('./models/user'));
mongoose.model('Character', require('./models/character'));
mongoose.model('Spell', require('./models/spell')

路线

app.get('/', function(req, res) {
  var User = db.model('User')
    , Character = db.model('Character')
    , Spell = db.model('Spell')
    , u = new User({username:'foo'})
    , c = new Character({name:'bar'});

  c.spells.push(new Spell({name:'fireball'}));
  c.spells.push(new Spell({name:'frozenball'}));
  u.characters.push(c);

  u.save(function(e, s) {
    User.find({username:'foo'}, function(err, success) {
      console.error(err);
      console.log(success);
    });
  });
});

控制台输出

null
[ { username: 'foo',
    _id: 4fda2c77faa9aa5c68000003,
    created: Thu, 14 Jun 2012 18:24:55 GMT,
    characters: [ undefined ] } ]

看起来 User 文档已被 mongoose 正确保存和检索。但是,关联的嵌入文档未定义

我想确保我的文档已保存,所以我直接向 mongodb 询问:

$ mongo mygame
> db.users.find({username:'foo'})
{
    "username" : "foo",
    "_id" : ObjectId("4fda2c77faa9aa5c68000003"),
    "created" : ISODate("2012-06-14T18:24:55.982Z"),
    "characters" : [
        {
            "name" : "bar",
            "_id" : ObjectId("4fda2c77faa9aa5c68000004"),
            "created" : ISODate("2012-06-14T18:24:55.986Z"),
            "spells" : [
                {
                    "name" : "fireball",
                    "_id" : ObjectId("4fda2c77faa9aa5c68000005"),
                    "created" : ISODate("2012-06-14T18:24:55.987Z")
                },
                {
                    "name" : "frozenball",
                    "_id" : ObjectId("4fda2c77faa9aa5c68000007"),
                    "created" : ISODate("2012-06-14T18:24:55.990Z")
                }
            ]
        }
    ]
}

如您所见,我的文档似乎已正确存储到 mongodb,但我无法检索嵌入了 mongoose 的 whose who。

我也尝试过不使用嵌套的嵌入式 Spell 文档,但会产生完全相同的问题。

编辑

@pat 你是对的。如果我以正确的顺序将所有模式直接放在同一个文件(例如主 app.js)中,它就可以工作。

事实上,我想尽可能将每个模型保存在单独的文件中(它们会增长很多)。

例如,我的用户模型包含在一个名为 models/user.js 的文件中,应该可以使用 module.exports 访问。

但是,当我尝试将我的模型链接到另一个文件中的 mongoose 时:mongoose.model('User', require('./models/user')); mongoose 查找方法返回未定义的嵌入文档。

对于如何将我的 Mongoose 模型正确保存在单独的文件中,您有什么想法吗?

最佳答案

用户模式文件应该首先在顶部要求 CharacterSchema,然后将其传入:

var CharacterSchema = require('./character');

var User = module.exports = new Schema({
      username: { type: String }
    , characters: [CharacterSchema]
    , created: { type: Date, default: Date.now }
});

同样,CharacterSchema 文件应该首先在顶部要求 SpellSchema,然后传递它:

var SpellSchema = require('./spell');
var CharacterSchema = module.exports = new Schema({ spells: [SpellSchema] })

这将保留他们的订单。

注意:子文档并不是真正需要成为模型,因为模型被映射到集合,但只要您不直接在子文档上调用 save,它们就不会保存到单独的数据库中的集合。

关于javascript - Expressjs Mongoose 查找嵌套嵌入文档未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039997/

相关文章:

javascript - 定期更新仅发生一次

node.js - 获取目录 node-fs 中包含信息(名称、类型、大小)的所有文件

node.js - Node 需要 Bluebird 失败

java - MongoDB 文档的 @Id 是 HashMap 的属性?

javascript - 如何在 MongoDB 中使用变量名递增嵌套对象

javascript - javascript中的彩色到灰度循环

javascript - Jquery.无法获取jSOn值

javascript - 在 REACTJS 中的函数中显示渲染结果

node.js - Heroku Node 应用程序不断崩溃

javascript - 在 JavaScript 中指定为自己的对象索引的对象作为字符串的好处