'Have trouble getting the toggle status from api & check it with status data
I have a toggle button with titles that is setting data to send to server.
And it is getting api data like what it is posted to server.
I want to check the if there is a similar item id in api data with rules then get its status and replace it with what it is right now.
But i get this error changedStatus is not defined
I would appreciate if someone can help me with it.
Link to my code in sandbox https://codesandbox.io/s/vuex-todo-list-forked-4kui26
<input type="checkbox" v-model="Rule.params.value" />
Rules: [
{
item_id: 144,
title: "_00_Init_Initialization",
params: {
value: false,
},
},
{
item_id: 145,
title: "_Init_AppsInitialization",
params: {
value: false,
},
},
{
item_id: 146,
title: "_02_Global_Generic",
params: {
value: false,
},
},
{
item_id: 147,
title: "_02_Global_Generic",
params: {
value: false,
},
},
{
item_id: 148,
title: "02_Global_Generic",
params: {
value: false,
},
},
{
item_id: 149,
title: "Global_Generic",
params: {
value: false,
},
},
],
apiData: [
{
item_id: 144,
params: {
value: true,
},
},
{
item_id: 145,
params: {
value: false,
},
},
{
item_id: 146,
params: {
value: true,
},
},
{
item_id: 147,
params: {
value: true,
},
},
{
item_id: 148,
params: {
value: false,
},
},
{
item_id: 149,
params: {
value: false,
},
},
],
in methods
mounted() {
console.log("mounted");
this.checkStatus();
},
methods: {
checkStatus() {
this.Rules.map((rule) => {
this.apiData.map((data) => {
const status = this.apiData.find(
(data) => rule.item_id == data.item_id
);
if (status.params.value == true) {
const changedStatus = this.Rules.find(
(data) => status.item_id == data.item_id
);
changedStatus.params.value = true
} else {
changedStatus.params.value = false
}
});
});
},
Solution 1:[1]
Try to define changedStatus outside of the condition:
methods: {
checkStatus() {
this.Rules.map((rule) => {
this.apiData.map((data) => {
const status = this.apiData.find(
(data) => rule.item_id == data.item_id
);
const changedStatus = this.Rules.find(
(data) => status.item_id == data.item_id
);
status.params.value == true ?
changedStatus.params.value = true :
changedStatus.params.value = false
}
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nikola Pavicevic |
