Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:34:39+00:00 2026-06-04T04:34:39+00:00

I’ve run into an interesting possible bug, but in this case it may be

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:34:40+00:00Added an answer on June 4, 2026 at 4:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.