Problem description: The code snippet (below) is not working for some reason. I don’t know if this is a glitch or something, because when I do it, it says id or name can not be defined.
Code snippet: const userid = getUserInfo(id)
and const user = getUserInfo(name)
Expected behavior: Find the userid and username.
Actual behavior: Error in console: id or user is not defined.
Steps to reproduce: ?
Bug appears at this link: https://replit.com/@MaxwellRose101/ChatterRe-Online-Chatroom
Browser/OS/Device: Chromebook
Please provide the replit link.
Edit
the problem is probably because you are using “const” where you should be using “let”
4 Likes
Looking at your code, you seem to misunderstand what the getUserInfo
function does. It gets the user info from a http request, meaning you cannot get the information of a random user using their name or id. You should use it like this:
app.use(function(req, res, next) {
const userInfo = getUserInfo(req);
next();
});
If you are trying the get the user’s id or name you would then access those properties from the userInfo
object:
app.use(function(req, res, next) {
const userInfo = getUserInfo(req);
console.log("id:", userInfo.id, "name:", userInfo.name);
next();
});
But if the user is not signed in, the return value will be null
, so you should be checking for that:
app.use(function(req, res, next) {
const userInfo = getUserInfo(req);
if (userInfo) {
console.log("id:", userInfo.id, "name:", userInfo.name);
}
next();
});