复合JS关系访问

我已经定义了2个模式对象如下(用于一个mongodb)

var User = describe('User', function () { property('name', String); property('email', String); property('password', String); set('restPath', pathTo.users); }); var Message = describe('Message', function () { property('userId', String, { index : true }); property('content', String); property('timesent', Date, { default : Date }); property('channelid', String); set('restPath', pathTo.messages); }); Message.belongsTo(User, {as: 'author', foreignKey: 'userId'}); User.hasMany(Message, {as: 'messages', foreignKey: 'userId'}); 

但是我无法访问相关的消息对象:

 action(function show() { this.title = 'User show'; var that = this; this.user.messages.build({content:"bob"}).save(function(){ that.user.messages(function(err,message){ console.log('Messages:'); console.log(message); }); }); // ... snip ... } }); 

尽pipe消息集合中添加了新消息,但消息数组始终为空。

我通过mongo shell运行了db.Message.find({userId:'517240bedd994bef27000001'}) ,并且显示了你所期望的消息,所以我开始想知道mongo适配器是否有问题。

CompoundJS中的一对多关系显示类似的问题(我认为)。

据我可以从文档中解决,这应该工作。 我究竟做错了什么?

编辑:

按照Anatoliy的build议对我的模式应用更改后,我放弃了我的mongo数据库并更新了npm,但是当我尝试创build一个新用户时,我得到了以下内容:

 Express 500 TypeError: Object #<Object> has no method 'trigger' in users controller during "create" action at Object.AbstractClass._initProperties (/mnt/share/chatApp2/node_modules/jugglingdb/lib/model.js:123:10) at Object.AbstractClass (/mnt/share/chatApp2/node_modules/jugglingdb/lib/model.js:31:10) at Object.ModelConstructor (/mnt/share/chatApp2/node_modules/jugglingdb/lib/schema.js:193:23) at Function.AbstractClass.create (/mnt/share/chatApp2/node_modules/jugglingdb/lib/model.js:222:15) at Object.create (eval at (/mnt/share/chatApp2/node_modules/compound/node_modules/kontroller/lib/base.js:157:17), :16:10).... 

编辑2:创build操作:

 action(function create() { User.create(req.body.User, function (err, user) { respondTo(function (format) { format.json(function () { if (err) { send({code: 500, error: user && user.errors || err}); } else { send({code: 200, data: user.toObject()}); } }); format.html(function () { if (err) { flash('error', 'User can not be created'); render('new', { user: user, title: 'New user' }); } else { flash('info', 'User created'); redirect(path_to.users); } }); }); }); }); 

这是ObjectID的问题。 在您的架构代码中:

 property('userId', String, { index : true }); 

所以userId是string,但是当你调用user.messages使用user.id(这是一个ObjectID)。 作为解决scheme,只需从模式定义中删除此行。

PS在你的情况下,你可以定义关系为:

 Message.belongsTo('author', {model: User, foreignKey: 'userId'}); User.hasMany('messages');