0

I have to load an array of data points. Each point have some properties. These are loaded and attached to point as property of object. Then send response back as array of points. But when the response arrives at the client, one of the property attached to each point does not arrive with response.

Router:

1.  ActivityDBRouter.get('/activityDB', (req, res) => {
2.    const filter = req.query.filter;
3.    const month = req.query.month;

4.    try {
5.      getActivityDB(filter, month, (err, db) => {
6.        if (err) {
7.          throw err;
8.        } else {
9.          res.json(db);
10.        }
11.      });
12.    } catch(err) {
13.      res.status(500).end();
14.    }

15.  });

When debugged, at line 9. I can see that property seasonValue attached to each point. But when response would arrive at client. That propery would be missing.

Ahmad Raza
  • 129
  • 2
  • 13
  • What data type does the `seasonValue` property contain? Is the `seasonValue` property enumerable (only enumerable properties are converted to JSON)? Can you show us what `console.log(db)` shows? Is `db` an array of objects? You can – jfriend00 Feb 15 '20 at 05:47
  • @jfriend00 Yes db is an array of objects, the mongoose `model` instances. – Ahmad Raza Feb 15 '20 at 05:49
  • It appears that mongoose model instances are not plain objects and you may need to convert them to plain objects before JSON serializing them. I don't know mongoose myself, but that's what I see when I look up the topic. – jfriend00 Feb 15 '20 at 05:53
  • @jfriend00 But I used to send these instance objects before this. Those does not create such an issue, even when further properties attached. I have even tried to `defineProperty` with this. Even that is not working. – Ahmad Raza Feb 15 '20 at 05:56
  • Try doing `console.log(Object.getOwnPropertyDescriptor(db[0], "seasonValue"))` and show us that result. I asked you what type of value that property contains and you haven't answered that yet either. – jfriend00 Feb 15 '20 at 06:00
  • Also, this is really old, but perhaps relevant: https://stackoverflow.com/questions/9952649/convert-mongoose-docs-to-json. It implies that you have to convert your array into an array of regular Javascript objects before it will serialize into JSON properly. – jfriend00 Feb 15 '20 at 06:02
  • Is `seasonValue` a `virtual` property? Read here: https://mongoosejs.com/docs/tutorials/virtuals.html#virtuals-in-json. – jfriend00 Feb 15 '20 at 06:05

0 Answers0