2

I've got this JSON snippet in a much much larger MongoDB document:

formConfig: {
  person: {},
  title: {name: 'title', ... },
  name: {name: 'name', ...}
}

However, when I then try to retrieve the document containing this JSON it doesn't return person: {} at all. All I'm getting is:

formConfig: {
  title: {name: 'title', ... },
  name: {name: 'name', ...}
}

Which completely breaks the frontend side of things since I need to know that person is there, regardless if it's empty or not.

When I search for this issue I can't find any similar questions or resources explaining why this happens in the first place, let alone how I can fix it.

So how do I fix it so it returns the person as well?

Here's the actual query, appConfig contains the JSON as mentioned:

exports.getSingle = (req, res, next) => {

  AppConfig.findOne({_id: req.params.id})
    .exec((err, appConfig) => {

      res.json({
        error: null,
        data: appConfig
      });
    }
  );
};
2
  • you can checkout the schema of AppConfig if there is anything called select: false for person. Commented Mar 7, 2018 at 11:47
  • try to console.log req.params.id to check if the value is correct Commented Mar 7, 2018 at 11:48

1 Answer 1

2

The issue was actually at Schema level, not using minimize: false in the Schema options cause empty objects to be removed.

This works:

new Schema ({...}, {minimize: false});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.