为什么可以从mongodb nodejs中的数据库中获取数据?
我正在尝试使用MongoDB进行简单的登录系统。 连接正常工作,但是当我尝试获取密码时:
async function getAdminPassword(username) {
const query = { username: username }
// console.log(admins)
const user = await admins.findOne(query);
try {
return user.password
}
catch (err) {
console.log(err);
}
}
它将给出此错误:
TypeError: Cannot read property 'password' of null
at getAdminPassword (C:\Users\isaia\programing\stinkysocks\chat\script.js:44:21)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
为什么这不起作用?提前致谢!
顺便说一句,这只是一个测试。请不要告诉我哈希密码,我知道我应该。
I'm trying to make a simple login system with mongodb.
Connecting works fine, but when i try to get the password:
async function getAdminPassword(username) {
const query = { username: username }
// console.log(admins)
const user = await admins.findOne(query);
try {
return user.password
}
catch (err) {
console.log(err);
}
}
It will give this error:
TypeError: Cannot read property 'password' of null
at getAdminPassword (C:\Users\isaia\programing\stinkysocks\chat\script.js:44:21)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
Here is my database, in case anyone wants it:
Why doesn't this work? Thanks in advance!
By the way, this is just a test. Please don't tell me to hash my passwords, i know i should.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑以下内容:
在当地打开您的MongoDB。
确保您的应用程序连接到DB/您是用户名
在您的数据库中是否存在。
可能会带有返回
的功能承诺{<待定> }
,其因为只要结果是
尚未解决。您必须致电。然后承诺捕获
结果不管承诺状态如何(已解决或仍在待处理)
您的完整代码:
我对保存密码的方式有点怀疑,请考虑使用加密算法。
不要忘记设置为用户名 unique:True 创建架构时。
Consider the following:
Turn on your MongoDb locally.
make sure your application is connected to db/ the username you are
looking for exist in your DB.( Do not forget to import the schema you are using
const admins= require('./models/yourAdminsModelJs')
)probably your function with return
Promise { <pending> }
, itsbecause promise will always log pending as long as its results are
not resolved yet. You must call .then on the promise to capture the
results regardless of the promise state (resolved or still pending)
your full code:
I am a litle bit suspicious about the way you are saving the password, consider using encryption algorithm.
Do not forget to set to username unique : true when creating the Schema.
我不能说太多,因为您提供的源代码在这里和那里都是一堆片段。
但是查看错误,这意味着您要查询的模型使用
用户名
,它是null的,这意味着未定义的用户名值或用户名在集合中找不到。阅读
req.body.username
的值时,您会得到什么?更新
这只是您如何适应代码的一个示例。我不确定您是如何收到请求的,但是您可以在此处引用我的请求。
如果您想保持获取函数以分别获取用户帐户,则可以尝试使用此功能。
如果它仍然不起作用,就像我提到的那样,您可以将您传递给功能并查看所获得的功能的console log
用户名
。然后检查集合中的用户名是否存在。我怀疑您所经过的用户名是否不正确。
除此之外,请在保存DB时哈希密码。
I can't really say much since the source code you provided are bunch of snippets here and there.
But looking at the error, it means the model you are querying for using
username
, it is null which means either the value for username is not defined or the username cannot be found in the collection.What do you get when reading the value of
req.body.username
?UPDATE
This is just an example of how you could adapt to your code. I am not sure how you get the request but you can refer to mine here.
If you want to keep the getting function to get user account separately then, you could try this.
If it's still not working, like I mentioned you can console log
username
that you are passing to the function and see what you get. Then check if the username exists in the collection.I suspect the username you are passing is somehow incorrect.
Apart from that, please hash your password when saving in db.