复合JS关系访问

时间:2013-04-21 14:20:38

标签: node.js compoundjs jugglingdb

我已经定义了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 ...
    }
});

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

我通过mongo shell运行db.Message.find({userId:'517240bedd994bef27000001'}),并按照您的预期显示消息,因此我开始怀疑the mongo adapter是否存在问题。

One to Many relationship in CompoundJS显示类似的问题(我认为)。

就我可以从文档中解决而言,这应该有效。我做错了什么?

修改

按照Anatoliy的建议将更改应用到我的架构后,我删除了我的mongo数据库并更新了npm但是当我尝试创建新用户时,我得到了以下内容:

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)....

EDIT2: 创建行动:

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);
                }
            });
        });
    });
});

1 个答案:

答案 0 :(得分:1)

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

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

所以userId是字符串,但是当你调用user.messages user.id时(它是一个ObjectID)。 作为解决方案,只需从架构定义中删除此行。

P.S。在您的情况下,您可以将关系定义为:

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