1

hello I'm working with mongoose and after a long time I still dont know how to fix this error... I tried during several hours to fix it but every time I just 'hide' the problem by complete the field with random character but for my project I cant, I need a precise Id so if someone could help with an example if he can, it will be really nice

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

I m trying to create an Id like : map1AssigneSeat

And to persist I use this code where mapId is equal to map1AssigneSeat:

var rowData = new MapDatabase({ 
  MapDescription:  mongoose.Types.ObjectId(mapId),
  DeckNumber: Number(map.Deck[i].DeckNumber),
  x: Number(map.Deck[i].x),
  y: Number(map.Deck[i].y),
});
Chris
  • 41
  • 6
  • Just leave Mongo id as is. If you need custom identifier, you could add `mapId` field. – Estus Flask Aug 03 '18 at 12:46
  • but to join two table I need an objectId no ? – Chris Aug 03 '18 at 13:00
  • You need id, not necessarily ObjectId. If you believe this is needed, you can replace id types. See https://stackoverflow.com/questions/21468443/relations-in-mongoose-with-custom-field – Estus Flask Aug 03 '18 at 13:12
  • ok but basically when I will try to join the index will be only on _id ? Because I need two Id for a collection in my project – Chris Aug 03 '18 at 14:08

1 Answers1

0

mongoose.Types.ObjectId always expect 24 characters that is a hexadecimal character like 5b6159a1034cf06b23aa2382 which is equivalent to 12 bytes. The error is due to incorrect length of 24 digits hexadecimal character. Check the value and length of mapId which you are passing in as a parameter. It must satisfy 24 digits hexadecimal characters to resolve that error.

You can however add some fixed characters like say you have 20 characters of mapId then add 4 characters of hexadecimals like abcd to make it total of 24 and use that.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62