2013-04-21 156 views
2

我已經定義了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); 
       } 
      }); 
     }); 
    }); 
}); 
+0

「創建」操作的代碼是什麼? – Anatoliy 2013-05-22 09:17:21

+0

@Anatoliy更新了問題:) – Spike 2013-05-22 16:55:36

回答

1

這是ObjectID的問題。在你的架構代碼:

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

所以用戶id是字符串,但是當你調用使用user.messages user.id(和它的一個對象ID)。 作爲解決方案,只需從模式定義中刪除此行。

P.S.在您的情況下,您可以將關係定義爲:

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

感謝您的幫助,我申請了上述內容,但重新啓動複合應用程序後問題仍然存在。所以我放棄了mongo數據庫並做了'npm update'。然後,我在'create'期間得到'TypeError:Object#'在用戶控制器中的Object.AbstractClass._initProperties(/mnt/share/chatApp2/node_modules/jugglingdb/lib/model.js:123:10)處沒有方法'觸發' 嘗試創建新用戶時的「操作」。 – Spike 2013-05-11 09:39:45