Meteor.collection will not update with string variables


#1

I’m hopeful someone can help me over this frustrating hurdle.

I have two string variables that will be passed to Meteor.Methods() for updating an existing collection document.

  1. updateCardState = “userDoing”;
  2. updateCardId = “mpZDE6KhSmhC7Lyfd”;

The following code will not update the collection document “Cards”.

Meteor.call(‘cardUpdate’, updateCardId, updateCardState);

Meteor.methods({
cardUpdate: function(cardid, cardstate) {
check(cardid, String);
check(cardstate, String);

Cards.update(cardid, {$set: {cardState: cardstate} });

The Meteor.call statement below works and the update is executed by substituting string literals for the variables, e.g.

Meteor.call(‘cardUpdate’, “mpZDE6KhSmhC7Lyfd”, “userDoing”);

I even tried changing Meteor.call to…

Meteor.call(‘cardUpdate’, String(updateCardId), String(updateCardState)); // this does not work either

My console.log confirms I’m sending strings in all cases:

  • updateCardId = “mpZDE6KhSmhC7Lyfd” – console.log from calling script.
  • updateCardState = “uDoing” – console.log from calling script.
  • “mpZDE6KhSmhC7Lyfd” – console.log from cardUpdate script.
  • “uDoing” – console.log from cardUpdate script.

There are no errors in the console or in the terminal session.

Help?

Thanks… Jim


#2

Are you able to reproduce the issue in a brand new meteor project? If so, put it up on GitHub as it may assist others with solving your problem.


#3

I created a new meteor project, but I’m still experiencing the same issue.

I did confuse part of the issue in my initial post.

The issue with updating is only with the document _id string that I pull from the DOM to create the “cardid” string variable. The cardid displays on the console.log and on alerts as a string. I also pull the “cardstate” string from the DOM.

When I attempt to update using the “cardstate” variable by using the “cardid” variable to find my document, the update fails. But there’s no errors reported anywhere, and both variables passed the “check test” – e.g. check(cardid, String); and check(cardstate, String);

If I hard code a string literal, e.g. – “jufRc3tC5v8sF7fJs” – or if I make the cardid variable = to “jufRc3tC5v8sF7fJs”, then the document is found and the update works with the “cardstate” value replacing the old value.

What makes this so confusing is the “cardid” variable that I pulled from the DOM is a string, too, by every method I’ve used to confirm. But, the “cardid” variable just doesn’t work.

Thanks… Jim


#4

I found my problem. I used applied JSON.stringify() to the cardid. So it’s interesting that the document _id field will accept a string literal when hard-coded, but requires an string object when using a variable.

Thanks for your help.

Regards… Jim


#5

If you run console.log(JSON.stringify(updateCardId)), what is printed in the console? Copy/paste that (everything, including the " at the ends) so we can see.


#6

Here is the console.log output…

updateCardId = savedArray[i].cardid; – DOM is the source
updateCardId = jufRc3tC5v8sF7fJs //works. created from the DOM without change.

updateCardId = JSON.stringify(updateCardId); – DOM is the source
updateCardId = “jufRc3tC5v8sF7fJs” //does not work. created from the DOM and then JSON.stringify

updateCardId = “jufRc3tC5v8sF7fJs”;
updateCardId = jufRc3tC5v8sF7fJs //works. created as a simple hard-coded string

This is the first time I encountered this issue – maybe because this is the first time I’ve used the DOM as input to variables.

Regards… Jim


#7

I’m a bit lost now, but when you use JSON.stringify, you get extra " around the id, so it becomes another id. How do you fetch the id from the DOM? And in Meteor, you shouldn’t do that (a better way usually exists), so if you show us your HTML and JavaScript code for the template, we may be able to help you better.


#8

Hello Peppe,

I appreciate your suggestion and your offer, but I’m early into this project. I’m really more of a hack than a programmer. I am a loyal Meteor advocate, but I’m still early in the learning curve. This particular project is a Kanban board. Multiple unordered lists serve as my columns and swimlanes, and I drag and drop list elements within a list and to other lists – and update on the drop. I’m sure I’m leaning on JQuery events more than I should – mostly because I didn’t really know how to do what I’m doing using Spacebars and Helpers as my primary go to tools. Many of the Event triggers need to be JQuery. Early on, I tried to reproduce some of these special purpose event triggers using Meteor Events, but I can’t get most of them to work properly.

I use the DOM to determine which of my lists received the list item dropped so that I can do a Meteor.call to update the drop list location in the collection. My Kanban board and cards function as planned. I just got briefly hung up on my update issue that prompted this post.

So, posting my code at this point is a bit premature since I’m learning as I go, and I’m sure I’ll do a rewrite once I have things working – e.g. I already know I can reduce my lines of code by using more functions than I’m presently using.

Thanks… Jim