我正在处理一个未读的消息计数器,并收集警报和消息。
现在问题是控制台日志显示警报附加到消息,但计数器没有显示。在console3e中键入Alerts.find().count();
得0。
模板计数器+点击清除提醒
//helper
alertCount: function(){
return Alerts.find({ userId: Meteor.userId(), read: false }).count();
}
//event
'click .enterRoom': function() {
Meteor.call('markAlertsAsRead', { roomId: this._id });
}
提交消息时附加提醒
'submit form': function(e, template) {
e.preventDefault();
var message = template.find('input').value;
var roomId = this._id;
var messageId = Messages.insert ({
roomId : Router.current().params._id,
msg : message,
userId : Meteor.user()._id,
});
template.find('input').value = '';
createMessageAlert(messageId, roomId, message);
var insertedMessage = Messages.findOne(messageId);
console.log(insertedMessage + messageId); //logs msg + alert
}
alert.js集合中的
Meteor.methods({
markAlertsAsRead: function(options){
check(options, Object);
Alerts.update({ roomId: options.roomId }, { $set:{ read: true } }, { multi: true });
}
});
createMessageAlert = function(messageId, roomId, message){
if ( message.user !== Meteor.userId() ){
Alerts.insert({
userId : message.userId,
roomId : Router.current().params._id,
messageId : messageId,
read : false
}, function(error, result){
if( error )
console.log( error );
if( result )
console.log( 'alert attached, alertId: ' + result );
//returns an alertId in my case
});
console.log('alert#: ' + Alerts.find().count()); //returns 0
}
};