0

I have pet data stored on as a mongodb object as shown below:

  {
    "_id" : ObjectId("0000439359317900131f111"),
    "name" : "Travis",
    "description" : "Travis is a support animal",
    "petId" : "225123",
    "petDetails" : {
        "color" : "brown"
    }
  }

When I try to console.log petDetails (petDetails is a map) object using the following:

const pet = await context.AnimalService.findOne({where:{_id : userSubscription.planId}})
const petDetails = pet.petDetails
console.log('Logging Pet Details', petDetails)

The result is returned as:

Logging Pet Details {
    '$__parent': '{\n' +
        '  _id: 0000439359317900131f111,\n' +
        "  name: 'Travis',\n" +
        "  description: 'Travis is a support animal',\n" +
        "  petId: '225123',\n" +
        "  petDetails: Map(1) { 'color' => 'brown' },\n" +
        '  __v: 0,\n' +
        '}',
    '$__path': 'petDetails',
    '$__schemaType': '[object Object]'

How can have petDetails simply returned as an object and not the weird way it is currently formatted?

1 Answers1

0

maybe your petDetail is Mongo object, you should convert it to plain object by using petDetail.toObject

Ref: https://stackoverflow.com/a/7503523/1743046

lehanh
  • 569
  • 7
  • 22
  • I get toObject is not a function error when I try converting it even though I am using the .findOne method – Olympus Sep 29 '22 at 05:17
  • @Olympus what lib you are using to connect with Mongo? can you tried `toJSON()` method, if still not works, I think you should attach file `package.json` for more info – lehanh Sep 29 '22 at 07:18