json - JavaScript Cannot set property of undefined -


i'm novice backend developer , i've been tasked adding react features , i'm totally out of depth. following code generates exception "cannot set property '1' of undefined". first element id:1 , name:"". don't think empty string has problem though since i've tried hardcoding string , same exception.

payment_code: {     label: 'payment code',     value: 'payment_code',     choices: adminsalesstore.paymentcodes.map((payment_code) => {         console.log(payment_code);         console.log(payment_code['id']);         console.log(payment_code['name']);         displaychoices.payment_code[payment_code['id']] = payment_code['name'];         return {             label: payment_code['id'],             value: payment_code['name']         }     }),     type: 'choice' }, 

i don't understand error because payment_code['id'] , payment_code['name'] not null or undefined. log statements confirm this. sorry super basic question i've googled issue pretty extensively , error still makes absolutely no sense. there's similar functions in same class , work. however, other methods use const arrays , need pull values db.

i'm guessing property show part of assignment displaychoices, i.e. it's like:

displaychoices = {     ...     payment_code: {         label: "payment code",         value: 'payment_code',         choices: adminsalesstore.paymentcodes.map((payment_code) => {             console.log(payment_code);             console.log(payment_code['id']);             console.log(payment_code['name']);             displaychoices.payment_code[payment_code['id']] = payment_code['name'];             return {                 label: payment_code['id'],                 value: payment_code['name']             }         }),         type: 'choice'     },     ... }; 

the problem until statement finishes, there's no displaychoices.payment_code property, can't assign displaychoices.payment_code[anything].

you should assignment after finish first assignment.

displaychoices = {     ...     payment_code: {         label: "payment code",         value: 'payment_code',         type: 'choice'     },     ... };  displaychoices.choices = adminsalesstore.paymentcodes.map((payment_code) => {     console.log(payment_code);     console.log(payment_code['id']);     console.log(payment_code['name']);     displaychoices.payment_code[payment_code['id']] = payment_code['name'];     return {         label: payment_code['id'],         value: payment_code['name']     } }), 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

http - Safari render HTML as received -