javascript cannot read property '0' of undefined in graphql query

Viewed 1118

I'm failing to set a value in this constant:

const targetedMessageTemplate = { 
    "query": createTargetedMessage(input: {
         conversationId: '',
         targetUserId: '',
         targetDialogId: '',
         annotations: [{
             genericAnnotation: {
                 title: '',
                 text: ''
             }
         }]
    }) 
};

When calling this:

var test= targetedMessageTemplate;

test.annotations[0].title = 'Test title';

Returns:

TypeError: cannot read '0' of undefined.

Setting the value of the conversationId variable works fine, however accessing the annotations array seems to be an issue.

Any hint appreciated on what I'm missing is highly appreciated. Thank you

1 Answers

genericAnnotation object key is not written.

test.annotations[0].genericAnnotation.title = 'Test title';

But error is different. Are you sure annotations isn't an empty array?

Related