gpt4 book ai didi

mongodb - 在 mongoDB 中使用 firebase Auth uid 作为用户文档 _id

转载 作者:行者123 更新时间:2023-12-05 02:07:32 25 4
gpt4 key购买 nike

我有用户集合,想使用 Firebase uid 作为文档 _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'); mongoose 返回

Failed: _id: Cast to ObjectID failed for value "ThobO6kTv6aMHUa4UN3nBjkCgNb2" at path "_id"

最佳答案

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

如果您计划将字符串用于 _id 值,请在架构中将 _id 显式声明为 String

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

关于mongodb - 在 mongoDB 中使用 firebase Auth uid 作为用户文档 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61708452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com