I will try to explain this as best as I can.
I have a select in my form which the user can chose between multiple contracts, once he choses one of them, I want to remove items from the selected contract before saving it.
The contract has and id, name, etc .. and a list of roles, for now I want to be able to keep only one role in that list so I will have to remove the other roles before saving.
Here’s what my select looks like:
<v-autocomplete
v-model="contract"
:items="contractsData"
item-text="name"
hide-details="auto"
light
solo
dense
clearable
return-object
></v-autocomplete>
The contract that is selected here is a an object that looks like this:
{
"id": "id-contract-001",
"name": "contract1",
"roles": [
{
"id": "id-role-001",
"name": "admin",
"description": "can view, create, update & delete"
},
{
"id": "id-role-002",
"name": "operator",
"description": "can view & create"
},
{
"id": "id-role-003",
"name": "user",
"description": "can only view"
}
]
},
Here’s the method I tried to do to keep only the role I want to keep:
getContractUserRole(contract) {
let rolesList = contract.roles
rolesList.forEach((role) => {
if (role.name === 'user') {
this.contract = {
id: contract.id,
name: contract.name,
roles: [
{
id: role.id,
name: role.name,
description: role.description,
},
],
}
}
})
},
And I am calling this method in my post request :
await this.$axios
.$post(uri, {
username: this.username,
firstname: this.firstname,
lastname: this.lastname,
email: this.email,
password: this.password,
contracts: this.getContractUserRole(this.contract),
I have to add that I can’t just put role.id
and role.name
to the role I want to keep because in each contract the role.id
is different so I have to search for the role in the selected contract.
When I try to use this method I get an error Cannot read properties of undefined (reading 'forEach')
, I know for a fact that all my contracts have roles so I don’t understand exactly what’s missing ? Maybe my approach is not the best, does anyone know the best way I can do this ?
EDIT: added more details to the question.
2
If you just want to change the post data trying the function below.
getContractUserRole(contract) {
let rolesList = contract.roles
const res = {
id: contract.id,
name: contract.name,
roles: []
}
rolesList.forEach((role) => {
if (role.name === 'user') {
res.roles.push(role)
}
})
return res
}
0