0

so i want to show all of groups that user have when logged in. But i just got 1 value only. I'm still newbie using node js

so here the code :

    var data = { id: user.id_panel_users, username: user.username, role: user.role, groups: user.groups }
    var [group] = await modelGroup.getUserGroup(data.id)

    for(let i=0;i<group.length;i++){
        data.groups = group[i].group_name;
    }


    console.log(group, data.groups)
    // data.toString();
    console.log("serialize user: ", data)
    done(null, data);

here the result :

[
  TextRow { group_name: 'add_url_premium' },
  TextRow { group_name: 'add_url_regular' }
] add_url_regular
serialize user:  {
  id: 77,
  username: '082122735123',
  role: 2,
  groups: 'add_url_regular'
}
4
  • What data structure is data.groups ? If it is array, you should push values in it, and it should work. Commented Apr 28, 2020 at 11:13
  • @LuckyLuke data.groups is a property of data objects.. its an object in passport.serializeUser for show all data of user Commented Apr 28, 2020 at 11:18
  • When you call await modelGroup.getUserGroup(data.id), does it return an array of arrays, or just a single array? When you use var [group] = ..., you expect the right-hand-side to return an array containing a single value, and you are assigning that value to the group variable. This might be another source of the problem. Commented Apr 28, 2020 at 11:19
  • @BillyBrown [ TextRow { group_name: 'add_url_premium' }, TextRow { group_name: 'add_url_regular' } ] this one is the result from var [group] = await ... Commented Apr 28, 2020 at 11:21

2 Answers 2

1

The main problem that I can see is in the for loop:

for (let i = 0; i < group.length; i++) {
    data.groups = group[i].group_name;
}

Every time, you are assigning the group name to the data.groups field. This changes the type from an array to a string and overwrites the previous value every time.

You should instead be using the Array.prototype.push function to add the group to the end of the array:

for (let i = 0; i < group.length; i++) {
    data.groups.push(group[i].group_name);
}

There is a cleaner way of doing this using the map function: it loops through an array and applies a function to each item, replacing that item in the array with the value that the function returned. In your case, your code would become:

let data = { id: user.id_panel_users, username: user.username, role: user.role, groups: user.groups }
let [group] = await modelGroup.getUserGroup(data.id);
data.groups = group.map(g => g.group_name);

I would also recommend that you use semi-colons ; at the end of all statements, and that you try to be consistent in your use of let/const and var.

Sign up to request clarification or add additional context in comments.

Comments

1

data.groups needs to be an array. What you are doing is instead of appending group_name to data.groups, you are assigning the string to it. So, instead of :

for(let i=0;i<group.length;i++){
    data.groups = group[i].group_name;
}

you should use:

for(let i=0;i<group.length;i++){
    data.groups.push(group[i].group_name);
}

Also, var is an old way of declaring variables in javascript. If you are new, I would suggest using let/const

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.