2014-12-05 137 views
0

試圖瞭解流星&中的CRUD有一個基本問題,那就是當我刪除自動發佈並使用明確的pub/sub時,來自客戶端的集合插入更新服務器而不是客戶端集合。Meteor.js:Collection.insert在服務器上工作,但不是客戶端

結果是,雖然正確獲取字段值,但插入在客戶端失敗。在服務器端,記錄插入正確。

$流星刪除自動發佈

創建HTML表單文件(它是有效的和預期的功能),則:

文件/server/publish.js:

Meteor.publish('todos'), function() { 
    return Todos.find(); 
} 

文件/lib/collections.js:

Todos = new Mongo.Collection('todos'); 

文件/client/subscribe.js:

Meteor.subscribe('todos'); 

文件/client/todos.js:

Template.todosList.helpers({ 
    todosList: function() { 
     return Todos.find(); 
    }, 
    }); 

Template.todoNew.events({ 
    'submit form': function(event) { 
     event.preventDefault(); 
     var theRecord = { 
      description: $(event.target).find('[id=description]').val(), 
      priority: $(event.target).find('[id=priority]').val() 
     }; 
     // Display correct field values, so form data is OK 
     console.log('Attemping to insert: ' + theRecord.description); 
     Todos.insert(theRecord, function(error) { 
      // This error always occurs 
      console.log('error inserting: ' + theRecord.description); 
     }); 
    } 
}); 

回答

1

爲了從客戶端寫入集合你將需要一個allow規則。把這樣的東西在/server

Meteor.publish('todos', function() { 
    return Todos.find(); 
}); 

Todos.allow({ 
    insert: function(userId, doc) { 
    // a todo must have a description and a priority 
    return (doc.description && doc.priority); 
    } 
}); 
+0

非常感謝 - 我仍然無法得到它的工作。我嘗試了幾個不同的版本,他們導致這樣的運行時錯誤:**調用方法'/ todos/insert'時發生異常TypeError:無法讀取null的屬性'description' 以下是我的嘗試: * *文件/server/publish.hs:** ' Todos.allow({ 插入:功能(用戶ID,DOC){ 回報(doc.description && doc.priority); } }); ' 人和: **文件/server/publish.hs:** 'Todos.allow({ 插入:功能(theRecord){ 回報(theRecord.description && theRecord.priority);} } ); – tomcam 2014-12-05 22:24:28

+0

@tomcam,第一個參數是用戶的id(如果用戶沒有登錄,則爲'null'),第二個參數存儲文檔。 – 2014-12-05 22:29:09

+0

這麼認爲,@ peppe-l-g,並且感謝您的回覆!因此,我的第二個版本,這也不起作用。我有意刪除了自動發佈,而不是**添加accounts-ui。在我看來,pub/sub在技術上與accounts-ui無關,儘管實際上它應該在任何有價值的應用程序中使用。 我錯了嗎?刪除自動發佈時,我是否總是必須添加accounts-ui? – tomcam 2014-12-05 22:35:49

相關問題