2016-02-12 88 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