Loopback-next: How to get a particular key of an object using the "fields" filter?

Created on 2 Apr 2020  路  7Comments  路  Source: strongloop/loopback-next

I have data in MongoDB that look like this:

"value": {
  "EN": "Please write to us for Queries",
  "UR": "爻賵丕賱丕鬲 讴蹝 賱卅蹝 蹃賲 倬乇 賱讴诰 讴乇蹖诤"
}

My model is:

@property({
  type: "object",
  required: true,
  default: ""
})
value: Object;

And I'm trying to get the value of EN with the field property:

{
  "where": {
    "additionalProp1": {}
  },
  "fields": {
    "_id": true,
    "language_id": true,
    "title": true,
    "code": true,
    "created_at": true,
    "updated_at": true,
    "status": true,
    "additionalProp1": {}
  },
  "offset": 0,
  "limit": 100,
  "skip": 0,
  "order": [
    "string"
  ]
}

So how to get value.EN via the field filter?

question

All 7 comments

@agnes512, could you please take a look? Thanks.

@meet02 The filter fields can include/exclude the properties from the result.

IIUC, you're trying to include the value property in the query result and to get the value of result.value.EN. If so, the property value needs to be set as true in the fields filter. ( You can not just include EN and exclude UR)

For example, your model has three properties:

export class Customer extends Entity {
  @property({
    type: 'string',
    id: true,
  })
  _id?: string;

  @property({
    type: 'string',
  })
  name?: string;

  @property({
    type: 'object',
  })
  value?: object;
  }
}

To include only _id and value, in the API Explorer, your filter will be:

{
    "fields": {
         "_id": true,
         "value": true
    }
}

The value property should be included in the result. Could you check if the filter works for you if you include the value property in the fields filter?

@agnes512 Thanks for answering, yes i am adding value in field , so i get value object but i want value as string

like my db have
"value":{
"EN":"Hello this is test example",
"HD":" Hello this is my second test example "
}

then i am passing
{
"fields": {
"_id": true,
"value": true
}
}
then i get value object but i want particular key value from that value object

what i do if i want EN value from value obejct

Can i access direct that value object key from filed

I want value=Hello this is test example not full value object

Would you not treat the object as normal? value.EN would access the property would it not?

@dougal83 After quring data. I can access all value object.

But i dont want value object I just want EN value

Using projection

My simple question is

How do I project or field value object keys not full object. I want only one key from that object not object

@meet02 I think there may be a language barrier issue here but this seems simple. Are either of these what you are looking for?

const myvalue = value.EN; or return value.EN (inside function that returns EN's type)?

Might be worth seeing if TS object tutorial contains what you are looking for because I'm confused.

Closing due to inactivity and since the question seems to be answered. Feel free to open a new issue if the problem persists.

Was this page helpful?
0 / 5 - 0 ratings