Mongoose:Coffeescript中的递归嵌入文档 [英] Mongoose: Recursive embedded-document in Coffeescript

查看:241
本文介绍了Mongoose:Coffeescript中的递归嵌入文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此示例(其工作原理):

  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

但是它返回一个错误, / p>

  TypeError:undefined不是函数
在CALL_NON_FUNCTION_AS_CONSTRUCTOR(native)
Function.interpretAsType /node_modules/mongoose/lib/schema.js:202:10)
在Schema.path(/path/node_modules/mongoose/lib/schema.js:162:29)
在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)
在Module._compile(module.js:411:26)
在Object.run(/ usr / local / lib / node_modules / coffee -script / lib / coffee-script.js:57:25)
在/usr/local/lib/node_modules/coffee-script/lib/command.js:147:29
在/ usr / local / lib / node_modules / coffee-script / lib / command.js:115:26



<好的,我发现当Person不是一个数组(在括号中),但是为什么?它不工作?

解决方案

嵌入的文档只能作为数组中的项存在。这是根据设计,您可以询问作者的原因:)



您可能想使用 DBRef

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

(注意您不需要添加调用)



请参见填充文件/ DBRef


Based on this example (which works):

var Comment = new Schema();

Comment.add({
    title : { type: String, index: true }
  , date : Date
  , body : String
  , comments : [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._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

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

解决方案

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.

这篇关于Mongoose:Coffeescript中的递归嵌入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆