meteorangular-meteoroptimistic-ui

Meteor's optimistic ui broken, when Meteor.publish with "createdAt: {$gte: start, $lt: now}"


Story: I've used meteor 1.4 with angular 1. And I want to make "Todo" private by using Meteor.publish, here is the client code:

var now = new Date();
var start = new Date();
start.setHours(0, 0, 0, 0);
this.helpers({
    todos() {
    return Todos.find({createdAt: {$gte: start, $lt: now}, userId: Meteor.userId()}, {
            sort: {
                createdAt: -1
            }
        });
    },
    currentUser() {
        return Meteor.user();
    }
});

Problem: When I used Meteor.publish with "userId", the todo list ui worked well, it could auto update ui after I added one new todo. But after I added Meteor.publish filter with createdAt: {$gte: start, $lt: now}, I have to refresh (F5) the page, then I can see the the new todo. Here is the server side publish code:

Meteor.publish('todos', function tasksPublication() {
     var now = new Date();
     var start = new Date();
     start.setHours(0, 0, 0, 0);
     return Todos.find({createdAt: {$gte: start, $lt: now}, userId: this.userId});
});

Anyone know how to fix it?


Solution

  • I've fixed this bug. As I wasn't deep into Meteor's cache, the reason below was my guess:

    1. Client UI was binging to cache(miniMongo)'s "Todos.find({createdAt: {$gte: start, $lt: now}, userId: this.userId})"
    2. After Inserting one new todo, it would insert to miniMongo firstly.
    3. miniMongo re-run #1 query, but the "now" is #1 time's "now". And Found the #2's result was not fit. So it removed the new record from client.

    And here is my change: Code Change

    if you have any different idea, please let me know.

    Thanks.