[SOLVED] Meteor React - Mongo object keys being removed inexplicably

EDIT: Problem resolved. There was an obscure method that was deleting keys. I still don’t know why my console logs were inconsistent, but I found what I am looking for.


I have been developing with Meteor for several months now, and I am stuck with this very bizarre glitch wherein keys are being removed out of my objects - even when I console log them and all the information is there, in the next line they will be gone even though it is the same exact object. I will show you what I mean.

I am pulling entries from a property (workHours) within a Mongo collection (Payrolls) that are JSON objects in the following format:

     hrpID: <string>
     fName: <string>
     lName: <string>
     regularHours: <string> (optional)
     vacationHours: <string> (optional)
     ptoHours: <string> (optional)

An array of objects in this format is being pulled from the Mongo db, then elements are rendered onto the page using this information. The problem that I am facing is that these objects appear to be getting stripped completely on their own without any input, causing elements on the page to disappear. The objects themselves are not getting deleted, rather several of their keys (fName, lName, and hrpID) are removed. The new format of these objects is as such (In most cases only one of these keys will be present):

     regularHours: <string> (optional)
     vacationHours: <string> (optional)
     ptoHours: <string> (optional)

It is changing my state without me calling this.setState, and changing declared variables without any input or direction from me. Hopefully this will make more sense when I show you what I have done to troubleshoot. For instance:

console.log(Payrolls.findOne({_id: this.props.payID}).workHours); // Outputs array of objects with all of their keys
let payrolls = Payrolls.findOne({_id: this.props.payID}).workHours;
console.log(payrolls); // Outputs array of objects that are MISSING almost all of their keys

I have also tried adding a Meteor method to get around this bizarre behavior. Here is the backend:

      let payrolls = Payrolls.findOne({_id:payID});
      console.log(payrolls.workHours); // Outputs array of objects with all of their keys
      return payrolls;

And on the client:

Meteor.call('getPayrolls', this.props.payID, (err, res) => {
                if (err){
                } else {
                    console.log(res.workHours); // Outputs array of objects that are MISSING almost all of their keys

I hope my problem makes sense now that I have posted some of my troubleshooting attempts. If it does not, please let me know what I have not made clear. I have been stuck on this problem for so long and I am dumbfounded as to why I console log one thing, then when I set the same exact thing to a variable, it is totally different. Or why my Meteor method return value is different on the server than on the client. I would appreciate any help, and if you need more information, please let me know.

I’m not sure about the first part, but at least in the method invocation part there’s an inconsistency in the method’s output vs. its usage on the client: the method returns the property workHours, yet the client accesses res.workHours. This seems very odd. Do the docs in Payrolls sometimes contain a nested property workHours.workHours?

1 Like

The Meteor function (server) was mistyped. It is correct now.

The docs in Payrolls contain a property workHours, which is the array of objects that I am seeking.