I have an angular app and the users can have multiple roles.
In the client when I remove or add a role to the user I send it to nodejs as an array of objects.
"roles": [
{
"role_id": 1,
"role_name": "Admin"
},
{
"role_id": 2,
"role_name": "Moderator"
}
],
my query :
for (i in roles) {
db.query(`
INSERT INTO user_roles (role_id, user_id)
VALUES($1, $2)
ON CONFLICT (role_id, user_id)
DO UPDATE
SET
role_id = $1,
user_id = $2`,
[roles[i].role_id, user_id], (err, data) => {
if (err) {
console.log(err)
} else {
console.log('inserted/deleted role')
}
})
}
Now, there are two issue:
1- when a user is created, they start with no roles and a null array of objects is being passed roles: [ null ] which will break the query because role.role_id is null.
2- If I want to remove a role, I am passing the below, but how do I add in the query that I want to remove the roles that don't exist in the array of objects?:
"roles": [
{
"role_id": 1,
"role_name": "Admin"
},
//the role moderator is removed from client so it's not being passed
],
NULLvalue. They are not the same thing.