1

I can create a survey with this body:

{
  "name":"name of survey",
  "description":"description of survet",
  "openAt":"2020-02-12T23:00:00.000Z",
  "closeAt":"2020-02-18T23:00:00.000Z",
  "questions":[..],
}

The server retrieve the user from jwt and store it as:

Screenshot of MongoAtlas document

screenshot of Survey document in MongoAtlas (only user part)

Now, if I edit my survey with this body:

{
  "createdAt":"2020-02-02T16:12:14.082Z",
  "_id":"5e36f4e008caf531ce848954",
  "name":"new name",
  ...
  "user":{
    "createdAt":"2020-02-02T16:12:14.063Z",
    "rank":0,"_id":"5e2f534422b6047e23c7f839",
    "_id":"5e2f534422b6047e23c7f839",
    ...
  }
}

My document is edited and _id type is become String type instead ObjectId

enter image description here

screenshot of Survey document in MongoAtlas (only user part)

Now this code doesn't work anymore because of the string type (when _id is ObjectId it does work)

await this.surveyModel.where('user._id').equals(user._id)

I'm using Angular 8 for frontend and Nest.js + TypeGoose for backend.

I don't know exactly how to fix this, may should I create a middleware that replace String _id to ObjectId ? Or should I use ObjectId in my frontend instead string ? Or should I use string by default with TypeGoose instead ObjectId ? Theses are the 3 solutions I imagine would do the works.

Pierre D
  • 327
  • 1
  • 2
  • 7

2 Answers2

0

Try to retrieve your data with the id. For example,

await this.surveyModel.where('user.id').equals(user.id)

Updated:

var id = parseInt(ObjectId.valueOf(), 16);

Reference: Documentation

Also check if this helps.

Angela Amarapala
  • 1,002
  • 10
  • 26
  • My model (neither survey or user) has not id property So if I do equals(user.id) I got "property id doesn't exist on type User) Should I create it ? Ur link doesn't help – Pierre D Feb 02 '20 at 18:07
  • No you shouldn't create one. It was to access the virtual id. Did you try using `ObjectId.valueOf()`? If not, kindly have a look at the updated answer. – Angela Amarapala Feb 02 '20 at 18:19
  • Where want U I use ValueOf objectId ? It's not vanilla mongo, it's TypeGoose so I cannot really follow the mongoose documentation. – Pierre D Feb 02 '20 at 18:28
0

When you use the default "_id" from mongoose it should be "ObjectId", and if it stores it as an string maybe try to set the id manually new mongoose.Types.ObjectId("Some24HexString")

Maybe you could update your question to include on how you save it to the db & your model used

hasezoey
  • 998
  • 9
  • 24