Turn Rowdatapacket into object to create login session

Asked

Viewed 657 times

2

Good morning.

I am making a Node.js application that when logging in you must save the user attributes (database attributes such as name, surname, email, id, etc). But when you have it displayed on the screen, it does not report errors, but also the user’s name. I am using "express-Session" and "cookie-parser".

In the search query, if found and supported user and password:

req.session.usuario = results; //criando sessao do usuario
res.redirect('/'); //redireciona para index agora logado

I took a test to see if Results was picking up the right data with a console.dir and everything is ok, retrieves the user attributes in an array in the index of the name in the database and recovered value(name: x, surname: y]

In the app js. application, there is an app.use function to instantiate the session:

app.use(function(req, res, next){
   res.locals.session  = req.session.usuario; //para exibir o nome do usuario na tela [partials - menu]
   console.dir("res.locals.session>>> "+res.locals.session);
   console.dir("req.session.usuario>>> "+req.session.usuario);
   res.locals.isLogged = req.session.usuario ? true : false; //se usuario logado = true, se nao = false [layout] 
   next();
});

I used the.dir consoles to try to see how the items inside the res.locals.Session e req.session.usuario, but in both just returns [Object Object]

My aim is to recover the id and user name for

1º - Display the user name in the menu:

if isLogged 
    li 
        a(id="tamanhoLinkMenu" href="/logout") Ola, #{session.nome} Logout

ps. isLogged works perfectly, because when the user is logged in appears "Hello, Logout", but note that the user name does not appear

2º - Recover the id to display to the user only the products that belong to it

If anyone knows what I’m doing wrong and can help or give a way forward, I’d appreciate it.

Big hug.


edited: Friends, I have found a solution to my problem.

It turns out that I am using the Mysql database and was following an example of a system that used Mongodb.

Mysql returns a array with the results and to create a user session apparently needs a object in order to be instantiated.

 Retorno MySQL visto no console:

 [RowDataPacket {
   id: 1,
   nome: x,
   sobrenome: y
} ]

To therefore save the data I need in the session, it is necessary to convert this array[] to a{} object. Unfortunately I could not do it by hand. I tried to use a function, some conversions to JSON, but none was only in object {} with indexes and values.

I made a small object in my hand and saved only the items I need immediately: ps.: Since the SQL query should return only 1 user, I know that the same is the index [0] of the results

UsuarioDados = {
   _id: results[0].usuario_id,
   nome: results[0].nome,
   sobrenome: results[0].sobrenome
}

And save the session with:

 req.session.usuario = UsuarioDados;

Now maybe the question changes a little: Does anyone know how to convert the Array [Rowdatapacket{Dice: value, Dice: value}] in one object only {Dice: value, Dice: value} in a way that works by removing the Rowdatapacket inscription?

  • By his examples the RowDataPacket is already an object in the format {chave:valor}. Replace the line: req.session.usuario = UsuarioDados; for req.session.usuario = results[0];.

  • Perfect Panther! Thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.