An array of objects is being destroyed on insert

This might have to do with me updating Meteor this morning.

Anyway here is some code:

//this returns Words properly    
console.log('splitter', splitSentenceIntoWordsForward(sentence)) 

var wordsForward = splitSentenceIntoWordsForward(sentence)

 //if I remove this line then everything works.
UserSentences.insert({user: Meteor.userId(), words: wordsForward})

//If I don't remove the line above, this prints an array of empty objects like this: [ { }, { } ]
console.log("wordsForward new", wordsForward, JSON.stringify(wordsForward))

It appears that insert is destroying the objects in the array somehow.

My original code has been working for weeks. Only this morning has it stopped. (There are other things besides meteor update that could be involved. I have added simpleschema and collection2. I also change some publish methods, but this code is fully run on the server.

This should work and I think the lack of response is because of that plus there must be something else going on. You need to dissect more: Does the UserSentences collection get inserted with the expected document? Have you tried simplifying the problem (just create a static array of two objects, insert it into UserSentences, and see if it’s changed after)? create a new, different collection and use that instead? Lift it out of your application environment - does that then work? Make wordsForward a const, what happens then?

1 Like

A const won’t make a difference. It just forbid rebinding, not array modification.

The Words array is also empty when I check the inserted document in UserSentences.

I just can’t fathom what is happening here… hold on.

(5 minutes later)

You all got me thinking, so I added {bypassCollection2: true} to the insert and now it works!

Thanks for your help.

I’m not sure why it wasn’t working, but I don’t need validation here so, I guess it will work until some time in the future when it doesn’t :wink:

1 Like