微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Mongoose:Coffeescript中的递归嵌入式文档

基于this example(有效):

var Comment = new Schema();

Comment.add({
    title : { type: String, index: true }
  , date : Date
  , body : String
  , comments : [Comment]
});

我想创建一个CoffeeScript版本

mongoose = require 'mongoose'
Schema = mongoose.Schema

Person = new Schema
Person.add
  mother: Person
  father: Person

但它返回错误,我不明白为什么

TypeError: undefined is not a function
    at CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
    at Function.interpretAsType (/path/node_modules/mongoose/lib/schema.js:202:10)
    at Schema.path (/path/node_modules/mongoose/lib/schema.js:162:29)
    at Schema.add (/path/node_modules/mongoose/lib/schema.js:110:12)
    at Object.<anonymous> (/path/Models/test.coffee:6:10)
    at Object.<anonymous> (/path/Models/test.coffee:10:4)
    at Module._compile (module.js:411:26)
    at Object.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script.js:57:25)
    at /usr/local/lib/node_modules/coffee-script/lib/command.js:147:29
    at /usr/local/lib/node_modules/coffee-script/lib/command.js:115:26

编辑:好的,我发现当Person不是数组(括号中)时它不起作用,但为什么呢?

解决方法:

嵌入式文档只能作为数组中的项存在.那是设计,你可以问the authors他们的原因:)

您可能想要使用DBRef:

Person = new Schema
  mother: { type: Schema.ObjectId, ref: 'Person' }
  father: { type: Schema.ObjectId, ref: 'Person' }

(注意你不需要添加电话)

docs for populate/DBRef.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐