Possible bug when observing a cursor, when deleting from collection

北城以北 提交于 2019-12-24 12:16:08

问题


I've run into an interesting possible bug, but in this case it may be caused by the lack of a function for me to use to delete a document when observing a collection. Or I am misusing observe... which could very well be the case!

Here is sample code that will reproduce the issue I'm having.

I'm using the devel branch as of this writing, so I'm not sure if this works in 0.3.5

observebug.html

<head>
    <title>observebug</title>
</head>

<body>
    {{> main}}
</body>

<template name="main">
    <h1>Example showing possible bug in Meteor wrt observe</h1>
    <div>
        <p>Try to delete a note. You will notice that it doesn't appear to get deleted. However, on the server, the delete did occur. Refresh the page to see that the delete did in fact occur.</p>
        <h2>Notes:</h2>
        <ul>
            {{#each notes}}
                {{> note_row}}
            {{/each}}
        </ul>
    </div>
</template>

<template name="note_row">
    <li>{{title}} <button name="delete">delete</button></li>
</template>

observebug.js

// CLIENT

if (Meteor.is_client) {
    Notes = new Meteor.Collection("notes_collection");

    Meteor.autosubscribe(function () {
        Meteor.subscribe("notes_subscription");
    });

    Template.main.notes = function () {
        return Notes.find();
    };

    Template.note_row.events = {
        "click button[name='delete']": function (evt) {
            Meteor.call("deleteNote", this._id, function (error, result) {
                if (!error) {
                    console.log("Note deletion successful.");
                } else {
                    console.log("Error when deleting note.");
                }
            });
        }
    };
}

// SERVER

if (Meteor.is_server) {
    Notes = new Meteor.Collection("notes_collection");

    Meteor.methods({
        "deleteNote": function (note_id) {
            try {
                Notes.remove(note_id);
                return true;
            } catch (e) {
                return false;
            }
        }
    });

    Meteor.publish("notes_subscription", function () {
        var notes = Notes.find({}, {sort: {title: 1}});
        var self = this;

        // What we're doing here is basically making an exact duplicate
        // of the notes collection. A possible use-case for this would be
        // to actually make changes to the copied collection on the fly,
        // such as adding new fields without affecting the original
        // collection.
        var upsertHandler = function (note, idx) {
            note.some_new_field = 100;
            self.set("notes_collection", note._id, note);
            self.flush();
        };

        var handle = notes.observe({
            added: upsertHandler,
            changed: upsertHandler,
            removed: function (note, idx) {
                // As far as I can tell, unset won't remove the document,
                // only attributes of the document. I don't think there's
                // a method to handle actually removing a whole document?
                self.unset("notes_collection", note._id);
                self.flush();
            }
        });

        self.onStop(function () {
            handle.stop();
            self.flush();
        });
    });

    // Add example notes
    Meteor.startup(function () {
        if (Notes.find().count() === 0) {
            Notes.insert({title: "Note #1"});
            Notes.insert({title: "Note #2"});
            Notes.insert({title: "Note #3"});
            Notes.insert({title: "Note #4"});
            Notes.insert({title: "Note #5"});
            Notes.insert({title: "Note #6"});
        }
    });
}

What you'll be seeing

When you start this application up, you'll see 6 example "notes", each with a delete button. I suggest having your console open so you can see the console.logs. Click the delete button on any note. The note does get deleted, however this doesn't get reflected back up to the client.

I suspect the issue lies in how I'm leveraging observe to create a copy of the collection (which I can then manipulate without affecting the original collection). I feel like I need a function that deletes a whole document, not just some attributes (unset).

EDIT: See the example in action over at http://observebug.meteor.com/


回答1:


So I've got it figured out. I did indeed need to use observe, and there is no Meteor bug on that end. Just a lack of understanding of what was involved in what I was trying to accomplish. Luckily I found a great starting point within the Meteor code itself and wrote an adjusted version of the _publishCursor function, which I've called publishModifiedCursor.

Here are the adjusted project templates and code:

observe.html

<head>
    <title>observe</title>
</head>

<body>
    {{> main}}
</body>

<template name="main">
    <h1>Example in trying to publish a modified copy of a collection</h1>
    <div>
        <h2>Notes:</h2>
        <ul>
            {{#each notes}}
                {{> note_row}}
            {{/each}}
        </ul>
        <p><button name="add">add note</button></p>
    </div>
</template>

<template name="note_row">
    <li>
        <strong>Original title:</strong> <em>{{title}}</em><br />
        <strong>Modified title:</strong> <em>{{__modified_title}}</em><br />
        <button name="delete">delete</button>
    </li>
</template>

observe.js

// CLIENT

if (Meteor.is_client) {
    ModifiedNotes = new Meteor.Collection("modified_notes_collection");

    Meteor.autosubscribe(function () {
        Meteor.subscribe("modified_notes_subscription");
    });

    Template.main.notes = function () {
        return ModifiedNotes.find();
    };

    Template.main.events = {
        "click button[name='add']": function (evt) {
            Meteor.call("addNote", function (error, result) {
                if (!error) {
                    console.log("Note addition successful.");
                } else {
                    console.log("Error when adding note.");
                }
            });
        }
    };

    Template.note_row.events = {
        "click button[name='delete']": function (evt) {
            Meteor.call("deleteNote", this._id, function (error, result) {
                if (!error) {
                    console.log("Note deletion successful.");
                } else {
                    console.log("Error when deleting note.");
                }
            });
        }
    };
}

// SERVER

if (Meteor.is_server) {
    Notes = new Meteor.Collection("notes_collection");

    Meteor.methods({
        "addNote": function () {
            try {
                Notes.insert({title: "Note #" + (Notes.find().count() + 1)});
                return true;
            } catch (e) {
                return false;
            }
        },
        "deleteNote": function (note_id) {
            try {
                Notes.remove(note_id);
                return true;
            } catch (e) {
                return false;
            }
        }
    });

    Meteor.publish("modified_notes_subscription", function () {
        // Pull up the original notes_collection
        var notes = Notes.find({}, {sort: {title: 1}});

        // Publish a near identical collection, with modifications
        this.publishModifiedCursor(notes, "modified_notes_collection", function (note) {
            note.__modified_title = getTitleModifiedByServer(note.title);
            return note;
        });
    });

    var getTitleModifiedByServer = function (title) {
        return title + "!!!";
    };

    // Add example notes
    Meteor.startup(function () {
        if (Notes.find().count() === 0) {
            Notes.insert({title: "Note #1"});
            Notes.insert({title: "Note #2"});
            Notes.insert({title: "Note #3"});
            Notes.insert({title: "Note #4"});
            Notes.insert({title: "Note #5"});
            Notes.insert({title: "Note #6"});
        }
    });

    _.extend(Meteor._LivedataSubscription.prototype, {
        publishModifiedCursor: function (cursor, name, map_callback) {
            var self = this;
            var collection = name || cursor.collection_name;

            var observe_handle = cursor.observe({
                added: function (obj) {
                    obj = map_callback.call(self, obj);
                    self.set(collection, obj._id, obj);
                    self.flush();
                },
                changed: function (obj, old_idx, old_obj) {
                    var set = {};
                    obj = map_callback.call(self, obj);
                    _.each(obj, function (v, k) {
                        if (!_.isEqual(v, old_obj[k])) {
                            set[k] = v;
                        }
                    });
                    self.set(collection, obj._id, set);
                    var dead_keys = _.difference(_.keys(old_obj), _.keys(obj));
                    self.unset(collection, obj._id, dead_keys);
                    self.flush();
                },
                removed: function (old_obj, old_idx) {
                    old_obj = map_callback.call(self, old_obj);
                    self.unset(collection, old_obj._id, _.keys(old_obj));
                    self.flush();
                }
            });

            self.complete();
            self.flush();

            self.onStop(_.bind(observe_handle.stop, observe_handle));
        }
    });
}

See it live over at http://observebug.meteor.com/. The final effect is underwhelming, since it's just a test involving adding/removing notes... but hey now it works!

Hope this helps someone else out in the future.




回答2:


this.unset requires three parameters, the first and second one are correct; it however requires a third parameter to tell which attributes to unset. This is a bug in your code and not in Meteor.

However, note that when your callback is called tha the document is already deleted from the collection and that you are just working on a copy of the object. Exactly, Notes.find({}).count() reveals the remaining count at any point during your callback.

// I don't think there's  
// a method to handle actually removing a whole document?

If you want two collections, create two collections. By only keeping one and trying to do all sorts of magic, you are calling functions that are not even meant to be doing what you want them to do. If you just create

Notes_copy = new Meteor.Collection("notes_collection_copy");

and use that to keep track of temporary notes, you won't run into any of the trouble you had now.



来源:https://stackoverflow.com/questions/10643509/possible-bug-when-observing-a-cursor-when-deleting-from-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!