问题
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