Mongoose - 无法使用 `findOrCreate` 创建超过 4 个字段 [英] Mongoose - Unable to create more than 4 fields using `findOrCreate`
问题描述
我正在使用 Node.js、MongoDB 和 Mongoose,并使用passport.js 进行身份验证.
I am using Node.js, MongoDB with Mongoose and am using passport.js for authentication.
这是我的用户架构:
const userSchema = new mongoose.Schema({
email: String,
password: String,
googleId: String,
facebookId: String,
profilePic: String,
fName: String,
lName: String
});
还有我的谷歌策略:
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/dashboard",
profileFields: ["id", "displayName", "photos", "email"]
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
console.log(profile.photos[0].value);
User.findOrCreate(
{ googleId: profile.id },
{ profilePic: profile.photos[0].value },
{ email: profile.emails[0].value },
function(err, user) {
return cb(err, user);
}
);
}
)
);
当我 console.log
我的结果时,我看到了我的个人资料,以及个人资料照片 URL 和个人资料电子邮件,但我看不到我的电子邮件 ID.只创建了 4 个字段:
When I console.log
my result I see my profile, along with profile photo url and profile email but I am unable to see my email id. Only 4 fields are getting created :
_id
googleId
profilePic
_v
谁能告诉我如何让电子邮件字段也保存?
Can someone tell me how to get the email field to save too?
推荐答案
您遇到问题的原因:
您没有很好地使用 findOrCreate
方法.findOrCreate
最多可以接受四个参数.findOrCreate(conditions, doc, options, callback)
:
Why you are having the issue:
You are not using the findOrCreate
method well. findOrCreate
can take up to four arguments.
findOrCreate(conditions, doc, options, callback)
:
conditions
:用于指定选择过滤器找到文档.doc
[可选]:如果没有找到匹配 selection-filter(conditions
) 的文档,则将这个doc
与你在conditions
中有什么然后插入到数据库中.options
[optional]:从插件代码库,我想你可以使用options.upsert
(如果设置为true
)更新文档,如果它已经存在.callback
:操作完成后执行的函数.
conditions
: This is used to specify the selection-filter to find the document.doc
[optional]: If a document that matches the selection-filter(conditions
) is not found, thisdoc
is merged with what is you have inconditions
and then inserted into the DB.options
[optional]: From the plugin codebase, I figured you can useoptions.upsert
(if set totrue
) to update the document if it already exist.callback
: The function executed after the operation is done.
你做错的是 passign { email: profile.emails[0].value }
作为第三个参数,其中 options
是预期的,你应该包括它在 doc
中,即第二个参数.
What you are doing wrong is passign { email: profile.emails[0].value }
as the third argument where options
is expected, you are supposed to include it in the doc
i.e the second argument.
修复
试试这个:
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/dashboard",
profileFields: ["id", "displayName", "photos", "email"]
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
console.log(profile.photos[0].value);
User.findOrCreate(
{ googleId: profile.id },
// Notice that this function parameter below
// includes both the profilePic and email
{ profilePic: profile.photos[0].value, email: profile.emails[0].value },
function(err, user) {
return cb(err, user);
}
);
}
)
);
这篇关于Mongoose - 无法使用 `findOrCreate` 创建超过 4 个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!