首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复合JS关系访问

复合JS关系访问
EN

Stack Overflow用户
提问于 2013-04-21 14:20:37
回答 1查看 850关注 0票数 2

我定义了2个模式对象,如下所示(用于mongodb)

代码语言:javascript
复制
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'});

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

代码语言:javascript
复制
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中运行了db.Message.find({userId:'517240bedd994bef27000001'}),并如您所期望的那样显示了消息,因此我开始怀疑蒙戈适配器是否有问题。

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

就我从医生那里得到的工作来说,这应该是可行的。我做错了什么?

编辑:

在按照Anatoliy的建议对我的模式进行更改之后,我删除了mongo数据库并更新了npm,但是当我尝试创建一个新用户时,我得到了以下内容:

代码语言:javascript
复制
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:创建操作:

代码语言:javascript
复制
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);
                }
            });
        });
    });
});
EN

回答 1

Stack Overflow用户

发布于 2013-05-08 17:24:14

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

代码语言:javascript
复制
property('userId', String, { index : true });

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

在你的例子中,你可以把关系定义为:

代码语言:javascript
复制
Message.belongsTo('author', {model: User, foreignKey: 'userId'});
User.hasMany('messages');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16132102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档