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

使用ejsmongoDBObjectId发生的错误

错误发生分析及原因

1.这里使用mongoDB中的ObjectId,通过构造器方式自行封装一个通过id查询数据的函数
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
2.使用ejs模版文件进行页面渲染,使点击相应链接,能从mongoDB数据库中通过id精准找寻到的相应数据Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
3.项目搭建成功后,在运行的初期,并没有提示任何错误,但是间隔一段时间之后,
控制台显示
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
这句话翻译成中文:
传入的参数必须是一个12字节的字符串或24个十六进制字符的字符串

错误就是在前端与后端数据交互的过程中,id长度出现了问题,不满足使用要求,所以报出错误。

错误解决

在前端对应id传回后端判断之前,先通过 toString() 将获得的id变成String,再传回后端,进行页面渲染,就可以解决问题。
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters