什么是“对象”在JavaScript Nodejs代码中?

发布于 2025-02-02 05:46:37 字数 1305 浏览 3 评论 0原文

我看到了以下代码:

const User = require('../model/User');
const jwt = require('jsonwebtoken');

const handleRefreshToken = async (req, res) => {
    const cookies = req.cookies;
    if (!cookies?.jwt) return res.sendStatus(401);
    const refreshToken = cookies.jwt;

    const foundUser = await User.findOne({ refreshToken }).exec();
    if (!foundUser) return res.sendStatus(403); //Forbidden 
    // evaluate jwt 
    jwt.verify(
        refreshToken,
        process.env.REFRESH_TOKEN_SECRET,
        (err, decoded) => {
            if (err || foundUser.username !== decoded.username) return res.sendStatus(403);
            const roles = Object.values(foundUser.roles);
            const accessToken = jwt.sign(
                {
                    "UserInfo": {
                        "username": decoded.username,
                        "roles": roles
                    }
                },
                process.env.ACCESS_TOKEN_SECRET,
                { expiresIn: '10s' }
            );
            res.json({ roles, accessToken })
        }
    );
}

module.exports = { handleRefreshToken }

我不明白这行代码中的对象是什么:

const roles = Object.values(foundUser.roles);

我的意思是是类的实例?什么课?它在哪里实例化?

对象参考什么?

I saw the following code:

const User = require('../model/User');
const jwt = require('jsonwebtoken');

const handleRefreshToken = async (req, res) => {
    const cookies = req.cookies;
    if (!cookies?.jwt) return res.sendStatus(401);
    const refreshToken = cookies.jwt;

    const foundUser = await User.findOne({ refreshToken }).exec();
    if (!foundUser) return res.sendStatus(403); //Forbidden 
    // evaluate jwt 
    jwt.verify(
        refreshToken,
        process.env.REFRESH_TOKEN_SECRET,
        (err, decoded) => {
            if (err || foundUser.username !== decoded.username) return res.sendStatus(403);
            const roles = Object.values(foundUser.roles);
            const accessToken = jwt.sign(
                {
                    "UserInfo": {
                        "username": decoded.username,
                        "roles": roles
                    }
                },
                process.env.ACCESS_TOKEN_SECRET,
                { expiresIn: '10s' }
            );
            res.json({ roles, accessToken })
        }
    );
}

module.exports = { handleRefreshToken }

And I can not understand what is the Object in this line of code:

const roles = Object.values(foundUser.roles);

I mean is it instance of a class? What class? Where did it instantiated?

What does this Object refer to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

最好是你 2025-02-09 05:46:37

node.js中的对象是卷曲括号内的任何东西,基本上是json。而且,如果您实际上阅读了node.js中的所有内容实际上是一个对象,但这是一个脱离话题。

那条代码行是在找到铸造师,并在“角色”类别下获取所有值,因此我认为它将寄回发现用户所具有的所有角色。

如果您想进行更多的研究,或者我还不够清楚地查看此页面: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/objects/object

An object in Node.JS is anything that's within curly brackets, JSON basically. And if you actually read into it everything in Node.JS is actually an object, but that's off topic.

What that line of code is doing is it is finding the foundUser, and getting all of the values under the 'roles' category, so I would assume it would be sending back all of the roles the found user has.

if you want to research more, or if I wasn't clear enough check out this page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

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