我不断收到此错误信息,当我点击发送按钮。我试图创建一个即时通讯应用程序,在线用户可以一对一聊天。我是一名初学者,我非常感谢任何帮助。这是我的错误消息,一旦我点击发送按钮,它就会出现在控制台中。流星:错误:{{#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,
});
}
});
什么是存储在该字段值的类型'messages'什么? – umesh
更具体地说,是一个数组还是一个虚假值?如错误所述,'{{#each}}块只接受数组,游标或错误的值。由于'messages'是聊天文档中的一个字段,因此如果消息的值为数组或falsey值,则可以在消息上使用{{#each}}。 – umesh
嗯我明白你在说什么。消息应该显示用户发送的每条消息(chat_message)。根据我的理解,消息应该将每个chat_message显示为一个数组。 chat_message模板中的错误可能导致它不能正确插入?导致#each不能正确读取。 – Jessica