3

I've been trying to debug a chunk of code for some hours now, banging my head against the wall, and finally pinpointed my issues to a place in the code where assigning the results of a collection.findOne() call to a variable is giving me different data than what I see with a console.log() of the same findOne() on the previous line.

prePostState = function(thisStID) {  
  console.log(Students.findOne({_id:thisStID}));  
  var stTemp = Students.findOne({_id:thisStID});  
  console.log(stTmp);  
  var testsTemp = stTmp.tests;

The collection object has a 'tests' array. In this instance, the array contains 3 objects as its elements.

While both the console.log() lines return something like this

 Object {_id: "eXf9dqQbaemKS24Ti", name: "Student,Name", group: "none", site: "SiteName", tests: Array[3]}  

Expanding each shows different data. The first one shows the correct tests: Array[3], the second one shows tests: Array[1], and the single element in that array also has data that is different from the matching element in the full array.

----Update----
Doing some further testing, I've changed the code a bit.

prePostState = function(thisStID) {
  console.log(Students.find({_id:thisStID}).fetch()); //1
  var stTmp = Students.find({_id:thisStID}).fetch();
  console.log(stTmp);                                 //2
  console.log(stTmp[0].tests.length);                 //3
  for(var i = 0; i < stTmp[0].tests.length; i++) {
     console.log(stTmp[0].tests[i]);                  //4
  }  

1 Returns:

[Object]
  0: Object
    _id: "AqLHB8hT8GxzQ7zyD"
    group: "none"
    name: "Student,Name"
    site: "SiteName"
    tests: Array[3]

2 Returns:

[Object]
  0: Object
  _id: "AqLHB8hT8GxzQ7zyD"
  group: "none"
  name: "Student,Name"
  site: "SiteName"
  tests: Array[1]

3 Returns:

3  

The for loop at 4 repeats three times and prints out each of the three objects in the tests array.

Obviously this means I can access the data I need. Instead of

var testArray = stTmp.tests;

Which leaves me with an array with only a single element, I will just have to get the length of stTmp.tests, and then use a for loop to access each element by index and insert them into the testArray variable.

So I can continue on, but I still don't understand the behavior I'm seeing. I'm on a bit of a timeline to keep making progress at this point, but when I have some time I may revisit this and try and replicate it in a meteorpad or other form that I can share the full code with.

soisystems
  • 194
  • 1
  • 8
  • I also tried var stTemp = Students.find({_id.thisStID}).fetch(); and thought for a minute it returned the correct data, but after a couple of Student.update() calls, it was returning the same incomplete/out of date data. – soisystems Apr 13 '15 at 22:48
  • Post a MeteorPad or share the repo, your question is too vague and I see typos on your console logs. – Mário Apr 14 '15 at 00:58
  • Are you sure they're actually different, and it's not just the browser console behaving strangely? In chrome if you go `x = {a: {}}`, `console.log(x)`, `x.a.b = 5` and only then expand the `console.log` output then you'll see `b` being 5, even though it wasn't set at the time of the `console.log`. Trying changing your code to `console.log(JSON.stringify(...))` instead of just `console.log(...)`. – user3374348 Apr 14 '15 at 15:15
  • No, I'm not sure. I changed a Collection.update() elsewhere and now my issue has disappeared. I tried reverting it back to test the JSON.stringify() vs just console.log(), but even with just console.log() I'm no longer getting the incomplete array, even with the update() reverted to how I had it before. Thanks for the JSON.stringify() tip though, I wasn't aware of that. Sorry that this apparently unreproduceable glitch wasted not just a bunch of my time but the time of folks here too. – soisystems Apr 14 '15 at 16:33

1 Answers1

0

1) If you modify a return value from Minimongo, don't expect it to persist. Minimongo was specifically written this way, so you are forced to use update operators to update the values.

2) The correct projection API is Coll.find({..selector..}, {fields:{..projection..}})

imslavko
  • 6,596
  • 2
  • 34
  • 43
  • This doesn't really address the issue. I am using collection.update to make changes, and console.log(Coll.find()) or findOne() show those changes. But assigning to a variable I get different data than what I had changed using update. – soisystems Apr 13 '15 at 22:46
  • @soisystems I cannot reproduce it on my machine. Perhaps a small app demonstrating an issue and an open GitHub ticket will be more appropriate here. – imslavko Apr 14 '15 at 00:26
  • Ok, thanks. I'll put something more specific together if I get a chance. I thought I might just be missing something obvious in how minimongo works, but it must be something specific to my code. – soisystems Apr 14 '15 at 15:04