最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose: Recursive embedded-document in Coffeescript - Stack Overflow

programmeradmin0浏览0评论

Based on this example (which works):

var Comment = new Schema();

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

I wanted to create a CoffeeScript version

mongoose = require 'mongoose'
Schema = mongoose.Schema

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

However it returns an error and I don't understand why

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._pile (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/mand.js:147:29
    at /usr/local/lib/node_modules/coffee-script/lib/mand.js:115:26

EDIT: Okay, I found out that it doesn't work when Person isn't an array (in brackets), but why?

Based on this example (which works):

var Comment = new Schema();

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

I wanted to create a CoffeeScript version

mongoose = require 'mongoose'
Schema = mongoose.Schema

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

However it returns an error and I don't understand why

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._pile (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/mand.js:147:29
    at /usr/local/lib/node_modules/coffee-script/lib/mand.js:115:26

EDIT: Okay, I found out that it doesn't work when Person isn't an array (in brackets), but why?

Share Improve this question edited Nov 3, 2011 at 20:52 nep asked Nov 3, 2011 at 8:51 nepnep 1152 silver badges5 bronze badges 1
  • The link above ("this example") is broken. The new URL is here: github./Automattic/mongoose/blob/master/examples/schema/… – Raffi Commented Sep 29, 2020 at 17:48
Add a ment  | 

1 Answer 1

Reset to default 6

Embedded documents can only exist as items in an array. That is by design, you can ask the authors for their reasons :)

You might want to use a DBRef:

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

(notice you don't need the add call)

See the docs for populate/DBRef.

发布评论

评论列表(0)

  1. 暂无评论