var criteria = Mongoose.Types.ObjectId(payloadData.skillId)
,
当我传递错误的Id
时,会出现以下错误消息。
错误:未捕获错误:传入的参数必须是单个字符串 12个字节或24个十六进制字符串
答案 0 :(得分:1)
mongo _id
是12字节的BSON类型ObjectId
var mongoose = require('mongoose');
var id = '1111adda111';
// var id = payloadData.skillId;
console.log(mongoose.Types.ObjectId.isValid(id)); // false
var cond = mongoose.Types.ObjectId.isValid(id)
if (cond) {
// do the required operation
} else {
console.log('not a valid id');
}