复合JS关系访问

编程入门 行业动态 更新时间:2024-10-23 07:32:31
本文介绍了复合JS关系访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经定义了以下两个模式对象(供在mongodb中使用)

I have defined 2 schema objects as below (for use in a 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'});

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

But I am unable to access the related messages object:

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

尽管有新消息添加到消息集合中,但消息数组始终为空.

Despite a new message being added to the message collection the array of messages is always empty.

我在mongo shell中运行了db.Message.find({userId:'517240bedd994bef27000001'}),并且按照您的期望显示了消息,所以我开始怀疑 mongo适配器.

I ran db.Message.find({userId:'517240bedd994bef27000001'}) through the mongo shell and that displayed the messages as you would expect, so I am begining to wonder if there is an issue with the mongo adapter.

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

One to Many relationship in CompoundJS Shows a similar issue (I think).

据我所知,这应该可行.我在做什么错了?

As far as I can work out from the docs, this should work. What am I doing wrong?

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

After applying the changes to my schema as suggested by Anatoliy I dropped my mongo database and updated npm but then when I tried to create a new user I got the below:

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

创建动作:

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的问题.在您的架构代码中:

It's an issue with ObjectID. In your schema code:

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

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

So userId is string, but when you call user.messages user.id used (and it's an ObjectID). As a solution just remove this line from your schema definition.

P.S.您可以将关系定义为:

P.S. in your case you can define relations as:

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

更多推荐

复合JS关系访问

本文发布于:2023-10-24 17:03:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!