Filter in loopback not working with mongodb object string id

402 views
Skip to first unread message

Manjeet Singh

unread,
Apr 26, 2017, 7:34:29 AM4/26/17
to LoopbackJS
I have a record in my database like this:

collection name: studentModels

{
  id:ObjectId("59008080165ce7345ee51e29"),
 "jobid":"59007cd5ece6e72f49cc910c"
}

When I use api of loopback like this


It returns 

  • name: "Error",
  •     status: 404,
  •     message: "Unknown "studentModels" id "undefined".",
  •     statusCode: 404,
  •     code: "MODEL_NOT_FOUND",
  •     stack: "Error: Unknown "studentModels" id "undefined". at Function.convertNullToNotFoundError ..........

And when I use the same api for searching id or any other property ex:


I am getting the correct record from the database.

My schema is like this

"jobid": {
      "type": "string"
    } 

and id is generated by mongodb.



Please help me this to solve my problem.

Francois Laforge

unread,
Apr 27, 2017, 9:54:44 AM4/27/17
to LoopbackJS
This might be related to the fact that loopback converts any input that "looks" like an ID (24 characters long, only digits or letters) into an ObjectId(), although the error message seems to suggest another problem.

For the "string to ObjectId" problem, you can solve it in 2 ways:
  1. Save your jobid as ObjectId("59007cd5ece6e72f49cc910c")
  2. Save your jobid as "_59007cd5ece6e72f49cc910c"
the 2nd way is a hack, but it will bust the conversion of your string to an object id.  Your query will look like this:  http://localhost:3000/api/studentModels/findOne?filter[where][jobid]=_59007cd5ece6e72f49cc910c

In my opinion, if jobid is a foreign key to another collection, then it should be saved in the same format as the primary key of your JOB collection.

Good luck!
Reply all
Reply to author
Forward
0 new messages