Mongoose: Recursive embedded-document in Coffeescript(Mongoose:Coffeescript 中的递归嵌入文档)
问题描述
基于 此示例(有效):
var Comment = new Schema();
Comment.add({
title : { type: String, index: true }
, date : Date
, body : String
, comments : [Comment]
});
我想创建一个 CoffeeScript 版本
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
好的,我发现当 Person 不是数组(在括号中)时它不起作用,但是为什么呢?
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 :)
您可能想要使用 DBRef
:
Person = new Schema
mother: { type: Schema.ObjectId, ref: 'Person' }
father: { type: Schema.ObjectId, ref: 'Person' }
(请注意,您不需要 add
调用)
(notice you don't need the add
call)
请参阅 有关填充/DBRef 的文档.
这篇关于Mongoose:Coffeescript 中的递归嵌入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mongoose:Coffeescript 中的递归嵌入文档
基础教程推荐
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01