2

in my code i have following console logs:

console.log(JSON.stringify(department.locationFilter, null, 2));
console.log(JSON.stringify(department.locationFilter.geoLocation, null, 2));

The first log results in:

{
  "city": "Stuttgart",
  "radius": 50,
  "geoLocation": {
    "type": "Point",
    "coordinates": [
      9.1800132,
      48.7784485
    ]
  }
}

The second log results in:

undefined

When i log "geoLocation" in department.locationFilter it also results in false.

I don't understand why the geoLocation is undefined because it exists inside the JSON object.

For some more information, the department is read from mongodb.

7
  • 1
    Do you use Mongoose? Because Mongoose is famous for generating weird and immutable objects. What you log is not necessarily JSON as you think but some of Mongoose's weirdos. Try to convert the Mongoose object to regular, plain object first : department = department.toObject() then try again Commented May 5, 2020 at 11:46
  • Does geoLocation have any invisible whitespace characters? Commented May 5, 2020 at 11:46
  • Is geoLocation available in all records? Commented May 5, 2020 at 11:47
  • Yes i'm using mongoose. There are no whitespaces Commented May 5, 2020 at 11:47
  • @JeremyThille your tip works. I'll accept it as answer. Strange thing because city and radius work, but geoLocation not Commented May 5, 2020 at 11:49

1 Answer 1

2

Mongoose is famous for generating weird and immutable objects. What you log is not necessarily JSON as you think but some of Mongoose's weirdos. Try to convert the Mongoose object to regular, plain object first : department = department.toObject().

Also, if you need only the data and not a full Mongoose object, you can query using .lean() :

Model.find({}).lean()

You'll get simple objects out of the box, and lean() is faster.

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.