使用firebase Auth uid作为mongoDB中的用户文档id



我有用户集合,希望使用Firebase uid作为document_id,

let _id = new ObjectID('Firebase Auth uid'); // Error Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex
let val = Object.assign(req.body, {
_id: _id,
});
const user = new User(val);
// Save User in the Collection
return user.save();

例如:Firebase Uid=5w9WnBiUPdT2wh7bsarYBQLQDqa2

如果我直接通过而不使用new ObjectID('Firebase Auth uid');猫鼬返回

失败:_id:值"转换为ObjectID失败;ThobO6kTv6aMHUa4UN3nBjkCgNb2";在路径"_id";

ObjectID是一个24字节的值,构造函数需要一个十六进制字符串。

如果计划将字符串用于_id值,请在模式中将_id明确声明为String

const schema = new mongoose.Schema({
_id: String
});

最新更新