Sunday 19 March 2017

Understing loops with node

In node js i have a function that iterates trough an array to fill another element. Some times some proprerties are undefined, how can that be? For example i get "cannot read property formattedPrice of undefined". What is wrong with a for loop on node?

for (var i = 0; i < 10; i++) {
    //console.log(JSON.stringify(item.SmallImage));
        message.attachment.payload.elements[i] = {
            "title":items[i].ItemAttributes.Title,
            "image_url": items[i].LargeImage.URL,
            "subtitle":items[i].ItemAttributes.ListPrice.FormattedPrice,
            "default_action": {
            "type": "web_url",
            "url": "https://www.google.it",
            "messenger_extensions": true,
            "webview_height_ratio": "tall",
            "fallback_url": "https://www.google.it"
            },
            "buttons":[
                {
                  "type":"web_url",
                  "url":"https://www.google.it",
                  "title":"View Website"
              },{
                  "type":"postback",
                  "title":"Start Chatting",
                  "payload":"DEVELOPER_DEFINED_PAYLOAD"
              }             
          ]      
        }
    //sendMessage( senderId, {text: item.ItemAttributes.Title+" PREZZO**:"+item.ItemAttributes.ListPrice.FormattedPrice});
    }



via Pjotr Raskolnikov

No comments:

Post a Comment