我已經定義了2架構如下對象(用於在MongoDB中)複合JS關係訪問
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顯示類似的問題(我認爲)。
據我可以從文檔工作,這應該工作。我究竟做錯了什麼?
編輯:
將更改應用到我的架構由阿納託利建議後,我放棄了我的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);
}
});
});
});
});
「創建」操作的代碼是什麼? – Anatoliy 2013-05-22 09:17:21
@Anatoliy更新了問題:) – Spike 2013-05-22 16:55:36