简体   繁体   中英

JavaScript convert [object Object] to JSON

I am implementing a token based authentication and I need to access the token I have set when i check if the user is authenticated.

function isAuthenticated(req, res, next) {
    const token = req.headers['authorization'];
    console.log(token);
    if (token) {
        req.token = token;
        next();
    } else {
        res.sendStatus(401);
    }
}

Unfortunately, the string "[object Object]"<\/code> doesn't have enough information to recreate back the original object. You'll have to debug the logic and find out why req.headers['authorization']<\/code> has such value in the first place.

"

Use JSON.stringify

function isAuthenticated(req, res, next) {
    const token = req.headers['authorization'];
    console.log(JSON.stringify(token));
    if (token) {
        req.token = token;
        next();
    } else {
        res.sendStatus(401);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM