Mongoose object id constraint

喜你入骨 提交于 2021-01-28 21:13:48

问题


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),
});

回答1:


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.



来源:https://stackoverflow.com/questions/51672423/mongoose-object-id-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!