gpt4 book ai didi

Calling findOne on a Schema with multiple uniques but only using one field in findOne's query will return not-found?(在具有多个唯一的架构上调用findOne,但在findOne的查询中只使用一个字段将返回NOT-FOUND?)

转载 作者:bug小助手 更新时间:2023-10-25 18:41:22 32 4
gpt4 key购买 nike



I'm practicing by following a React, MUI, Nodejs, CRUD project. Currently am able to get the register new user feature working but stuck on the next part where user logs in with the newly registered credentials.

我正在通过遵循Reaction、MUI、NodeJS、CRUD项目进行练习。目前我能够让注册新用户功能工作,但卡在下一个部分,用户登录与新注册的凭据。


So the data I'm trying to query is:

因此,我尝试查询的数据是:


Schema - User

架构-用户


















userName (unique, required) password (required) email (unique,required)
dog123 someLongSaltedHash [email protected]


Request JSON
{
userName : "dog123",
password : 'somePasswordPreHash',
}

请求JSON{用户名:“dog123”,密码:‘ome PasswordPreHash’,}


import mongoose from "mongoose";

const UserSchema = new mongoose.Schema(
{
userName: {
type: String,
required: true,
min: 1,
max: 50,
unique: true,
},
password: {
type: String,
required: true,
min: 8,
max: 50,
},
email: {
type: String,
required: true,
min: 5,
max: 50,
unique: true,
},
},
{
timestamps: true,
}
)

const User = mongoose.model("User", UserSchema);

export default User;

I have a model with multiple fields that have unique:true (the email and userName). When I run a controller that calls findOne({ userName: username }) and email is not provided, it always says not-found.

我有一个具有多个字段的模型,这些字段具有唯一的:True(电子邮件和用户名)。当我运行一个调用findOne({用户名:用户名})的控制器并且没有提供电子邮件时,它总是显示Not-Found。


export const login = async(request, response) => {
try {
// Deconstruct user data from request body
const {
userName,
password,
} = request.body;

// Query one User with matching userName
**const queriedUser = await User.findOne({
userName: userName,
})**

// If no User matched the userName, return not-found
if (!queriedUser) {
return response.status(400).json({
msg: "Credentials not found, please double-check and retry later.",** // this gets returned**
})
}
// Some other code handling the passwords

// Report success, and return token
response.status(200).json({
token: token,
user: queriedUser,
});
} catch (error) {
response.status(500).json({msg: error.message});** // no error thrown**
}
}

The documentation for mongoose on constraints didn't really talk about what if there's multiple 'uniques' and one was not provided.

Mongoose on Constraints的文档并没有真正讨论如果有多个‘唯一’而没有提供一个的情况。


Grateful for any insight into this before I go back and refactor the model, maybe removing one of the uniques.

感谢在我回去重构模型之前对这一点的任何见解,也许会删除其中一个独特之处。


I tried looking up docs on how unique works but there's little info on how findOne handles being passed only data for one of the uniques.

我试着查阅了Unique如何工作的文档,但几乎没有关于findOne如何处理被传递的数据的信息,只有一个Unique的数据。


I have verified that the User table does indeed have one item in it, and the usernames do match. The key is also correct "userName".

我已经验证了USER表中确实有一个条目,并且用户名确实匹配。密钥也是正确的“用户名”。


更多回答

Have you tried to console.log(request.body) to see if userName actually has 'dog123' in there? Having multiple unique values should have absolutely no effect on the Model.findOne() method. If you pass in the correct key:value pair to the function and the document exists like you said then it will 100% find the document for you.

您有没有尝试过sole.log(quest.body)来查看用户名中是否真的有‘dog123’?具有多个唯一值应该绝对不会影响Model.findOne()方法。如果您将正确的key:Value对传递给函数,并且文档如您所说的那样存在,那么它将100%为您找到该文档。

优秀答案推荐
更多回答

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