为什么可以从mongodb nodejs中的数据库中获取数据?

发布于 2025-02-08 04:59:10 字数 834 浏览 1 评论 0原文

我正在尝试使用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:
a part of my database

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吖咩 2025-02-15 04:59:10

考虑以下内容:
在当地打开您的MongoDB。

  1. 确保您的应用程序连接到DB/您是用户名
    在您的数据库中是否存在。

  2. 可能会带有返回的功能承诺{<待定> },其
    因为只要结果是
    尚未解决。您必须致电。然后承诺捕获
    结果不管承诺状态如何(已解决或仍在待处理)

您的完整代码:

const admins = require('./models/adminsModelJs')
..
..
..
async function getAdminPassword(username) {
    const query = { username: username }
    // console.log(admins)
    const user = await admins.findOne(query);
    // console.log(user)
    try {
        // console.log(user.password)
        return user.password
    }
    catch (err) {
        console.log(err);
    }
}

const result = getAdminPassword("someExample")
console.log(result)

result.then(function (result) {
    console.log(result) // "someExample password"
})

我对保存密码的方式有点怀疑,请考虑使用加密算法。

不要忘记设置为用户名 unique:True 创建架构时。

Consider the following:
Turn on your MongoDb locally.

  1. 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'))

  2. probably your function with return Promise { <pending> }, its
    because 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:

const admins = require('./models/adminsModelJs')
..
..
..
async function getAdminPassword(username) {
    const query = { username: username }
    // console.log(admins)
    const user = await admins.findOne(query);
    // console.log(user)
    try {
        // console.log(user.password)
        return user.password
    }
    catch (err) {
        console.log(err);
    }
}

const result = getAdminPassword("someExample")
console.log(result)

result.then(function (result) {
    console.log(result) // "someExample password"
})

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.

云淡月浅 2025-02-15 04:59:10

我不能说太多,因为您提供的源代码在这里和那里都是一堆片段。

但是查看错误,这意味着您要查询的模型使用用户名,它是null的,这意味着未定义的用户名值或用户名在集合中找不到。

阅读req.body.username的值时,您会得到什么?

更新

这只是您如何适应代码的一个示例。我不确定您是如何收到请求的,但是您可以在此处引用我的请求。

const Admin = require('../models/admins'); // This is import the model

exports.getAdminAccount = (req, res, next) => {
    const adminUserAccount = req.body.username;
    Admin.findOne({username: adminUserAccount})
        .then(adminUser => {
            // you can do your login functions here
            });
        })
        .catch(err => {
            // you can do your login functions here for error
        });
};

如果您想保持获取函数以分别获取用户帐户,则可以尝试使用此功能。

const Admin = require('../models/admins'); // This is import the model

async function getAdminPassword(username){

    const adminAccount = await Admin.findOne({username: username});
    if(!adminAccount){
        // your logic here if not account exist
        return;
    }

    //your logic here if account exists
    const adminPassword = adminAccount.password;
    return adminPassword;
}

如果它仍然不起作用,就像我提到的那样,您可以将您传递给功能并查看所获得的功能的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.

const Admin = require('../models/admins'); // This is import the model

exports.getAdminAccount = (req, res, next) => {
    const adminUserAccount = req.body.username;
    Admin.findOne({username: adminUserAccount})
        .then(adminUser => {
            // you can do your login functions here
            });
        })
        .catch(err => {
            // you can do your login functions here for error
        });
};

If you want to keep the getting function to get user account separately then, you could try this.

const Admin = require('../models/admins'); // This is import the model

async function getAdminPassword(username){

    const adminAccount = await Admin.findOne({username: username});
    if(!adminAccount){
        // your logic here if not account exist
        return;
    }

    //your logic here if account exists
    const adminPassword = adminAccount.password;
    return adminPassword;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文