Can't Access Images in Node

darryl_ssdarryl_ss Member Posts: 63
edited June 18 in Apps & Integrations

Hi,

I'm struggling to access images in Node. Would anyone mind telling me what is wrong with my path please?

itemID: item.itemID,
        customSku: item.customSku,
        qoh: parseInt(item.ItemShops.ItemShop[0].sellable),
        price: item.Prices.ItemPrice[0].amount,
        images: item.Images.Image.filename
Tags:

9 comments

  • gregaricangregarican Member Posts: 454 

    Aren't images an array? And also the images are accessed via load_relations in the API request I believe.


    Here attached is a sample API request/response pair that pulls images. Maybe this helps?


  • Adrian SamuelAdrian Samuel Moderator, Lightspeed Staff Posts: 608 moderator

    Hey @darryl_ss,


    You need to need to call the Item Image endpoint to get the image url for a given item: https://developers.lightspeedhq.com/retail/endpoints/Item-Image/

    Adrian Samuel

    Integrations & Solutions Developer - Strategic Solutions

    Lightspeed HQ

  • darryl_ssdarryl_ss Member Posts: 63

    Hi @Adrian Samuel & @gregarican ,

    I am accessing using relations. I'm not seeing an array for Images?

    {
      "@attributes": {
        "count": "1"
      },
      "Item": {
        "itemID": "881",
        "systemSku": "210000000881",
        "defaultCost": "18.48",
        "avgCost": "0",
        "discountable": "true",
        "tax": "true",
        "archived": "false",
        "itemType": "default",
        "serialized": "false",
        "description": "ADVANCED MAG WELL GRIP BLACK",
        "modelYear": "0",
        "upc": "",
        "ean": "",
        "customSku": "FX MOJOB",
        "manufacturerSku": "FAB",
        "createTime": "2020-03-24T09:43:16+00:00",
        "timeStamp": "2020-03-24T13:43:16+00:00",
        "publishToEcom": "false",
        "categoryID": "42",
        "taxClassID": "1",
        "departmentID": "0",
        "itemMatrixID": "0",
        "manufacturerID": "33",
        "seasonID": "0",
        "defaultVendorID": "26",
        "Images": {
          "Image": {
            "imageID": "149",
            "description": "",
            "filename": "FX-MOJOB-1000x1000.jpg",
            "ordering": "0",
            "publicID": "inatfp75ejy0fzbk4oqv",
            "baseImageURL": "https:\/\/res.cloudinary.com\/lightspeed-retail\/image\/upload\/",
            "size": "333088",
            "createTime": "2020-06-13T19:50:17+00:00",
            "timeStamp": "2020-06-13T19:50:17+00:00",
            "itemID": "881",
            "itemMatrixID": "0"
          }
        },
        "ItemShops": {
          "ItemShop": [
            {
              "itemShopID": "2642",
              "qoh": "0",
              "sellable": "0",
              "backorder": "0",
              "componentQoh": "0",
              "componentBackorder": "0",
              "reorderPoint": "0",
              "reorderLevel": "0",
              "timeStamp": "2020-03-24T13:43:16+00:00",
              "itemID": "881",
              "shopID": "1",
              "m": {
                "layaways": "0",
                "specialorders": "0",
                "workorders": "0"
              }
            },
            {
              "itemShopID": "2641",
              "qoh": "0",
              "sellable": "0",
              "backorder": "0",
              "componentQoh": "0",
              "componentBackorder": "0",
              "reorderPoint": "0",
              "reorderLevel": "0",
              "timeStamp": "2020-03-24T13:43:16+00:00",
              "itemID": "881",
              "shopID": "0",
              "m": {
                "layaways": "0",
                "specialorders": "0",
                "workorders": "0"
              }
            }
          ]
        },
    
  • gregaricangregarican Member Posts: 454 

    If I recall that was a gotcha. Most standard API responses will always define an array as an array. Even if it contains a single element. Like "Images": [ { "Image": {"blahblahblah"} } ] In the case of the Retail API, if the array has a single element then the brackets are dropped. As in your example code, "Images": { "Image": {"blahblahblah"} }

  • darryl_ssdarryl_ss Member Posts: 63

    Ok, thanks @gregarican. So it should be - item.Images[0].Image.filename?

  • gregaricangregarican Member Posts: 454 

    I don't use Node, so I'm not 100% sure. But I do know for my C# project I had to perform a bunch of checks against these properties, to check if the values were an array or just a singleton based on the JSON response. I finally created a generic method that would handle these. Too bad that Lightspeed doesn't adhere to standardized ways of handling this.

  • darryl_ssdarryl_ss Member Posts: 63
    edited June 18

    Thanks for the help @gregarican, I'm new to this game, but my experience of Epos api's has been that it's never the easy way ;)

  • gregaricangregarican Member Posts: 454 
    edited June 18

    Here is how I handle this using JSON.NET in the C# world. You could translate something similar to your Java/JavaScript world I'm sure :)


    class SingleOrArrayConverter<T> : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(List<T>));
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JToken token = JToken.Load(reader);
            if (token.Type == JTokenType.Array)
            {
                return token.ToObject<List<T>>();
            }
            return new List<T> { token.ToObject<T>() };
        }
    
        public override bool CanWrite
        {
            get { return false; }
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
    


    And another caveat is some fields that have NULL values aren't even returned in the API response at times. If your handlers are looking for consistent data set properties appearing in each API response then that can break things. As discussed here --> https://community.lightspeedhq.com/en/discussion/1936/inventory-transfer-json-response-missing-fields.

    Post edited by gregarican on
  • darryl_ssdarryl_ss Member Posts: 63

    Funnily enough, the beginner that I am. This issue was related to null values and I needed to filter them out, damn it. Ha! I will learn sooner or later ;)

    Thanks for the info it is much appreciated!

Sign In or Register to comment.