2016-02-12 89 views
1

我不斷收到此錯誤信息,當我點擊發送按鈕。我試圖創建一個即時通訊應用程序,在線用戶可以一對一聊天。我是一名初學者,我非常感謝任何幫助。這是我的錯誤消息,一旦我點擊發送按鈕,它就會出現在控制檯中。流星:錯誤:{{#each}}目前只接受數組,指針或falsey值

Exception from Tracker recompute function: meteor.js:862 Error: {{#each}} currently only accepts arrays, cursors or falsey values. at badSequenceError (observe-sequence.js:148) at observe-sequence.js:113 at Object.Tracker.nonreactive (tracker.js:597) at observe-sequence.js:90 at Tracker.Computation._compute (tracker.js:331) at Tracker.Computation._recompute (tracker.js:350) at Object.Tracker._runFlush (tracker.js:489) at onGlobalMessage (meteor.js:347)

這裏是我的HTML

<template name="chat_page"> 
    <h2>Type in the box below to send a message!</h2> 
    <div class="row"> 
    <div class="col-md-12"> 
     <div class="well well-lg"> 
     {{#each messages}} 
      {{> chat_message}} 
     {{/each}} 
     </div> 
    </div> 
    </div> 
    <div class="row"> 
    <div class="col-md-12"> 
     <form class="js-send-chat"> 
     <input class="input" type="text" name="chat" placeholder="type a message here..."> 
     <input type="submit" value="Send"> 
     </form> 
    </div> 
    </div> 
</template> 

<!-- simple template that displays a message --> 
<template name="chat_message"> 
    <div class = "container"> 
    <div class = "row"> 
     <img src="/{{profile.avatar}}" class="avatar_img"> 
     {{username}} said: {{text}} 
    </div> 
    </div> 
    <br> 
</template> 

客戶端服務器端的

Template.chat_page.helpers({ 
    messages: function() { 
    var chat = Chats.findOne({ _id: Session.get("chatId") }); 
    return chat.messages; 
    }, 
    other_user: function() { 
    return ""; 
    } 
}); 

Template.chat_page.events({ 
    'submit .js-send-chat': function (event) { 
    console.log(event); 
    event.preventDefault(); 
    var chat = Chats.findOne({ _id: Session.get("chatId") }); 
    if (chat) { 
     var msgs = chat.messages; 
     if (! msgs) { 
     msgs = []; 
     } 
     msgs.push({ text: event.target.chat.value }); 
     event.target.chat.value = ""; 
     chat.messages = msgs; 
     Chats.update({ _id: chat._id }, { $set : { messages: chat } }); 
     Meteor.call("sendMessage", chat); 
    } 
    } 
}); 

零件

Meteor.publish("chats", function() { 
    return Chats.find(); 
}); 

Meteor.publish("userStatus", function() { 
    return Meteor.users.find({ "status.online": true }); 
}); 

Meteor.publish("userData", function() { 
    if (this.userId) { 
    return Meteor.users.find({ _id: this.userId },{ fields: { 'other': 1, 'things': 1 } }); 
    } else { 
    this.ready(); 
    } 
    return Meteor.users.find({ "status.online": true }); 
}); 

Meteor.publish("users", function() { 
    return Meteor.users.find({ "status.online": true }); 
}); 

Chats.allow({ 
    insert: function() { return true; }, 
    update: function() { return true; }, 
    remove: function() { return true; } 
}); 

Meteor.methods({ 
    sendMessage: function (chat) { 
    Chats.insert({ 
     chat: chat, 
     createdAt: new Date(), 
     username: Meteor.user().profile.username, 
     avatar: Meteor.user().profile.avatar, 
    }); 
    } 
}); 
+0

什麼是存儲在該字段值的類型'messages'什麼? – umesh

+0

更具體地說,是一個數組還是一個虛假值?如錯誤所述,'{{#each}}塊只接受數組,遊標或錯誤的值。由於'messages'是聊天文檔中的一個字段,因此如果消息的值爲數組或falsey值,則可以在消息上使用{{#each}}。 – umesh

+0

嗯我明白你在說什麼。消息應該顯示用戶發送的每條消息(chat_message)。根據我的理解,消息應該將每個chat_message顯示爲一個數組。 chat_message模板中的錯誤可能導致它不能正確插入?導致#each不能正確讀取。 – Jessica

回答

0

機會是你的訂閱是沒有準備好。這意味着Chats.findOne()將不會返回任何內容,這意味着Chats.findOne().messages將不確定。

嘗試以下操作:

{{ #if Template.subscriptionsReady }} 
    {{#each messages}} 
    {{/each}} 
{{/else}} 

另外,在該聊天中的消息使用上聊天一個find(),然後{{#each}}。例如:

Template['Chat'].helpers({ 
    chats: function() { 
    return Chats.find(Session.get('chatId')); // _id is unique, so this should only ever have one result. 
    } 
}); 
模板

然後:

{{#each chats}} 
    {{#each messages}} 
    {{>chat_message}} 
    {{/each}} 
{{/each}} 
+0

如果訂閱是沒有準備好,不會'Chats.findOne()'是不確定的和訪問'Chats.findOne()。messages'給undefined'錯誤,而不是錯誤的的'不能讀取屬性「消息」 OP報道? – umesh

+0

另外,即使Chats.findOne()。消息是未定義的,它仍然不能作爲未定義是一個falsey值是有效的值,只要'{{#each}}'塊而言解釋錯誤。 – umesh

0

我覺得可能在此行

Chats.update({ _id : chat._id }, { $set : { messages : chat } }); 

你是外地messages的值設置爲chat是一個邏輯上的錯誤。但chat是一個對象。因此,在你的助手,當你返回Chats.findOne().messages{{#each}}塊,你實際上是返回一個對象,它是不被髮送到{{#each}}塊,因此錯誤的有效值。

我想你的意思做的是

Chats.update({ _id : chat._id }, { $set : { messages : msgs } }); 
+0

非常感謝。這確實奏效,至於消除錯誤,但我的消息仍然沒有出現,但當我路由到主,我得到另一個錯誤,我相信這是什麼阻止消息出現。 – Jessica

+0

模板助手中的異常:TypeError:無法讀取未定義的屬性'messages' – Jessica

+0

我明白了。在'messages'助手中,嘗試用'return chat && chat.messages'替換'return chat.messages'這一行。 – umesh