猫鼬对象ID约束 [英] Mongoose object id constraint
问题描述
您好,我正在使用猫鼬,但很长一段时间后,我仍然不知道如何解决此错误...我尝试了几个小时来解决它,但是每次我通过隐藏字段来隐藏"问题时,随机字符,但是对于我的项目,我不能做到,我需要一个精确的ID,因此如果有人可以,请提供一个示例,这将非常好
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
错误:传入的参数必须是12个字节的单个字符串或一串24个十六进制字符
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
我正在尝试创建一个ID,例如:map1AssigneSeat
I m trying to create an Id like : map1AssigneSeat
为持久起见,我使用以下代码,其中mapId等于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),
});
推荐答案
mongoose.Types.ObjectId
总是期望24个字符,它们是十六进制字符,例如 5b6159a1034cf06b23aa2382
到12个字节.该错误是由于长度不正确的24位十六进制字符引起的.检查作为参数传递的 mapId
的值和长度.它必须满足24位十六进制字符才能解决该错误.
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.
不过,您可以添加一些固定的字符,例如说您有20个字符的 mapId
,然后添加4个十六进制的字符(例如 abcd
),使其总数为24,并使用它.
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.
这篇关于猫鼬对象ID约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!