0

I'm running a function that needs fetch data from API and push into an array the fetch is with in units loop

     var newArray = [];
        units.forEach((value) => {
            var discount = [];
            axios.get(baseApi() + `GetAllDiscounts?intUnitTypeId=${value.UnitTypeId}`, {
                headers: { 'Content-Type': 'application/xml; charset=utf-8', 'Access-Control-Allow-Origin': '*' }
            }).then(response => {
                parseString(response.data, function (err, result) {
                    result.SSM.Record.forEach(function (content) {
                        if (content.DonotDisplay[0] === "False") {
                            discount.push({
                                discount_id: content.DISCOUNTID[0],
                                description: content.DISCOUNTNAME[0],
                                amount: content.Value[0],
                                typeText: content.CUSTOMERTYPE[0],
                                type: content.Type[0]
                            })
                        }
                    })
                })
            }).catch(error => { console.log(error) })


            console.log(discount)
            newArray.push({
                "ApproximateSize": value.ApproximateSize,
                "Breadth": value.Breadth,
                "CategoryName": value.CategoryName,
                "Discount": value.Discount,
                "InternetPrice": Math.round(value.InternetPrice),
                "InternetPriceAvailable": value.InternetPriceAvailable,
                "IsAvailable": value.IsAvailable,
                "Length": value.Length,
                "Rent": Math.round(value.Rent),
                "UnitTypeCode": value.UnitTypeCode,
                "UnitTypeDescription": value.UnitTypeDescription,
                "UnitTypeDetailedDescription": value.UnitTypeDetailedDescription,
                "UnitTypeId": value.UnitTypeId,
                // "discount_id": discount[0].discount_id,
                // "description": discount[0].description,
                // "amount": discount[0].amount,
                // "type": discount[0].type,
            })
        });

the result of discount array is [] but when I open array it contain one object also i check length it show 0 but it contain one array.

Note: I need signal object from api for every units

what am i do wrong? enter image description here

  • I think it might be because you've got a string that wasn't ended: 'GetAllDiscounts?+...' should be 'GetAllDiscounts?'+... instead, I think. – Lemondoge Oct 31 '20 at 17:41
  • 1
    @Lemondoge That would cause the script to stop with a `SyntaxError` hence this is probably just a typo – Andreas Oct 31 '20 at 17:43
  • Are you sure more than one value is returned in `result.SSM.Record`? The length is one, but the array index is zero based, so you see `0:` because that's the value at index zreo – audzzy Oct 31 '20 at 17:46
  • @Lemondoge typo mistake i am getting data from api but when i push within fetch it behave like image which i mention above but if i push dummy data outside of fetch then its correct – Vikas Sharma Oct 31 '20 at 17:48
  • thank you @audzzy but result.SSM.Record have 3 record always i have to pick one and push to discount array yes it pushed but like image i mentioned above which is not correct – Vikas Sharma Oct 31 '20 at 17:59
  • And are you sure the other two answer to the statement `content.DonotDisplay[0] === "False"`? – audzzy Oct 31 '20 at 21:09
  • Wait, you want just one object and for it to not be an array? – audzzy Oct 31 '20 at 21:10
  • I cant add an answer, but if I understand the last comment try changing `discount.push({...})` with `discount = {...}` and then you'll just have the one discount object, not in an array – audzzy Oct 31 '20 at 21:14

1 Answers1

-1

It's hard to tell what you're asking. However, better debugging will probably help you see what's going on. Here are some tips:

  • When you console.log an array, the console only shows a reference to that array, so if it's modified, the console will update also. If you want to see what it looks like at a point in time, one trick is to convert it to json and back like console.log(JSON.parse(JSON.stringify(discount)))
  • If you want to inspect several variables at a point in time to see what's going on, you can set a breakpoint or put debugger; in your code to activate chrome's debugger.
  • Step through your code and add console logs at each stage to verify that the data looks like you expect.
frodo2975
  • 10,340
  • 3
  • 34
  • 41