2

Help! I don't what am doing wrong, when I try to update an existing field using the $set method the entire document gets removed.

Can you kindly point out what I am doing wrong in my code:

recipientsDetails.update({_id: "GCYmFqZbaaYD7DvMZ"}, {$set: {paymentStatus: "Approved"}});

Thanks for your help!

SirBT
  • 1,580
  • 5
  • 22
  • 51
  • 3
    This code is correct. – Styx Sep 11 '17 at 14:48
  • 1
    You may therefore post the rest of the method, otherwise it will be impossible to solve this. – Jankapunkt Sep 11 '17 at 14:51
  • @Styx can you try running it in your browser console. It deletes even when I copy and paste and run it in my browser console. – SirBT Sep 11 '17 at 15:00
  • @Jankapunkt `Template.paymentB2C.events({ 'click #approvePaymentButton'(event, instance) { var selectedRecipient = this._id; recipientsDetails.update({_id: selectedRecipient}, {$set: {paymentStatus: "Approved"}}); }); ` – SirBT Sep 11 '17 at 15:01
  • @SirBT I did run it in my `mongodb` console and it works as expected. How were you able to run it in your _browser_ console? – Styx Sep 11 '17 at 15:04
  • @Styx I use Google Chromium. In the settings I open More tools/Developer tools. Developer console opens up in the browser. I am able to run the javascript commands in the console – SirBT Sep 11 '17 at 15:11
  • @SirBT Just to be sure, it works on client side as well: [Screenshot](https://d3vv6lp55qjaqc.cloudfront.net/items/0P053A0r371P1T3P3L1m/Screen%20Shot%202017-09-11%20at%206.08.19%20PM.png) – Styx Sep 11 '17 at 15:11
  • @Styx Something very strange is happening. If I update $set a new field (that doesn't exist) in my document e.g. `recipientsDetails.update( { _id: "oePJnT6SREdoA9JNx" }, { $set: { paymentStatus: "Approved"} } )` instead of `recipientsDetails.update( { _id: "oePJnT6SREdoA9JNx" }, { $set: { paymentStatusX: "Approved"} } )` this works. I am even able to successfully. Any idea why this happening? – SirBT Sep 11 '17 at 15:24
  • 2
    Is there any subscription listening to the changes of the doc, especially this field? – Jankapunkt Sep 11 '17 at 15:52
  • @SirBT You should update your question and add all relevant code that could affect this collection. Otherwise, we could only guess what is happening there. – Styx Sep 11 '17 at 16:00
  • @Jankapunkt Oh... thank you! You are a genius! Silly me for not figuring it out. YES It was definitely the subscription permitting only the paymentStatus = Pending in `recipientsDetails.find({paymentStatus:"Pending"});` to show, however whenever I updated to: paymentStatus = approved in `recipientsDetails.find({paymentStatus:"Approved"});` it stopped showing. I always assumed it was "removed", when it was merely hidden. Thanks once again – SirBT Sep 12 '17 at 08:23

2 Answers2

3

The code is correct. It's likely that your publish function for recipientsDetails contains recipientsDetails.find({paymentStatus: "Not Approved"}). Naturally, once you update the document, the document will no longer satisfy that filtering query and the document vanish from the client.

DoctorPangloss
  • 2,994
  • 1
  • 18
  • 22
  • Good guess, but in the answer @SirBT is saying that document is not vanishing from collection, but becomes replaced (instead of updating), so I don't think that's the reason. Anyway, we wouldn't know better until he provides additional information. – Styx Sep 12 '17 at 04:16
  • Turned out to be a great guess, nice work @DoctorPangloss – Michel Floyd Sep 12 '17 at 16:50
1

Your code is correct.Check you mongoDB using Robomongo tool.connect your local project with robomongo and update a document then check whether it is updated or not? If the record updated there is an issue with the publish or subscriptions

Thusila Bandara
  • 285
  • 4
  • 22