简体   繁体   中英

Update/Insert to Meteor collection being ignored by server

I have a collection called "Materials" defined on the client and server. In one template, I can insert and update correctly to Materials. On another template, I can insert and update but when I refresh the browser, the changes are lost.

The two views are called

view_materials (inserting/updating works)

view_orders (doesn't work)

Both templates have the materials collection binded to them like so:

//Bind viewOrders to Materials collection
Template.view_order.materials = function () {
return Materials.find();
};

// Bind materialsTemplate to Materials collection
Template.view_materials.materials = function () {
return Materials.find();
};

and both are using the function below to update.

var docid = Materials.findOne({material_number: newMaterial.material_number});
console.log(docid._id);
Materials.update({_id:docid._id},{$set: {material_qty: total}});

Please note that the ID printed out to the console matches perfectly. Like I mentioned, on view_orders it updates for a moment on the client but not in another browser window nor does it persist after being reloaded from the server. On view_materials it works perfectly. Any ideas?

I also deployed an example of the bug here: http://upexmple.meteor.com/

and added the source to github: https://github.com/stereochromatic/update_example

The relevant code can be found in client/views/view_materials/view_materials.js and client/views/view_orders/view_orders.js

To duplicate the error:

Click on inventory and under Raw Materials, type A for material number and -50 for quantity. You will see it get updated correctly. Now click on create release and under raw material type, select A and -50 for quantity. You will see the correct info get printed to the console and you may also see the changes on inventory but upon refresh those changes are gone. - show quoted text -

You need to define your Meteor Collections outside of the client directory. I usually put mine into a /lib folder, but defining collections on just the client side can cause issues.

So just move

Materials = new Meteor.Collection("materials");

and

MaterialsLog = new Meteor.Collection("materialsLog");

into a folder outside of the client/server folders.

I can't reproduce the error. I've done as you said, but the changes do not disappear for me upon refresh. I can even close the tab and reopen it, and the changes are still there.

Finally figured this out...

I removed the autopublish package with

meteor remove autopublish

and then defined my permissions (and subscriptions) in /lib/Materials.js like so:

// Declare client materials collection
Materials = new Meteor.Collection("materials");

materials = Materials

if (Meteor.isClient) {

    Meteor.subscribe('materials');


}

if (Meteor.isServer) {

    Meteor.publish('materials', function() {
        return materials.find();
});


materials.allow({

    insert: function (document) {
        return true;
    },
    update: function () {
        return true;
    },
    remove: function () {
        return false;
    }

    });

}

Thanks to everyone who helped out. I realize that for a production app this is best practice anyways so this needed really to be done at some point. I originally was going to wait until the end to remove autopublish, insecure and wrap collection in publish/subscribe, allow/deny rules but this issue helped expedite that work :)

Using your code above my with file structure: Please take note that I remove the materials = Materials

client folder:

subscriber.js

Meteor.subscribe('materials');

server folder

publisher.js

Meteor.publish('materials', function() {
    return Materials.find();

});

allow.js

Materials.allow({

insert: function (document) {
    return true;
},
update: function () {
    return true;
},
remove: function () {
    return false;
}

});

}

collection folder. this is outside from the client and the server folders

collections.js

Materials = new Meteor.Collection("materials");

let me know if its works and though this very late at least people can see it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM