0

I do not understand this one.

invoiceList shows one record.

LENGTH: shows 1

But it still triggers if (!invoiceList.length) {

    show({invoiceList})

    show("LENGTH: " + invoiceList.length)

    if (!invoiceList.length) {
        reject({code:"MULTI INVOICE ERROR", err:"You need to make an invoice"});
        return;
    }

How come that !invoiceList.length is triggered when length is 1?

I just tried this:

    var invoiceList = [
        {
            "foo": "bar",
        },
        {
            "foo": "bar",
        },
        {
            "foo": "bar",
        }
    ]

    console.log(invoiceList)

    console.log("LENGTH: " + invoiceList.length)
    console.log("LENGTH: " + invoiceList.length)

    if (!invoiceList.length) {
        reject({code:"ERR 1", err:"You need to make an invoice"});
    }
    if (invoiceList.length<1) {
        reject({code:"ERR 2", err:"You need to make an invoice"});
    }
    if (!invoiceList.length) {
        reject({code:"MULTI INVOICE ERROR", err:"You need to make an invoice"});
    }
    return;

And console.log gives me the following:

[ { foo: 'bar' }, { foo: 'bar' }, { foo: 'bar' } ]
LENGTH: 3
LENGTH: 3
{ code: 'MULTI INVOICE ERROR',
  err: 'You need to make an invoice with the word PRICE in one of the lines' }
getInvoice: [object Object]

So now I am really confused?!?!

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53
  • More info needed, better if you can you reproduce the same in a fiddle? Otherwise, others have to guess. – Mritunjay Aug 02 '17 at 11:56
  • Yes, you are right - must be somewhere else in the code that causes this. WIll give it another look – torbenrudgaard Aug 02 '17 at 11:58
  • Is `invoiceList` getting populated using a Promise/Async call? – Satpal Aug 02 '17 at 11:58
  • Too many missing pieces to give an accurate answer, however, the most logical explanation is your logging is happening asynchronously meaning by the time it's actually piped out `invoiceList` has been populated. – James Aug 02 '17 at 12:09
  • @Satpal yes it is, could that be the reason? – torbenrudgaard Aug 02 '17 at 12:13
  • I updated the code - now I am really confused ?!?! – torbenrudgaard Aug 02 '17 at 12:20
  • Cannot reproduce the latter result: error is left missing as it should. – jsalonen Aug 02 '17 at 12:22
  • I guess the console output is confusing you: object properties are only loaded in the dev console asynchronously, so they show the situation after `console.log` was executed. To avoid this from happening do `console.log(JSON.stringify(invoiceList));` That will output the content as it *is at that very moment*. For the reason why `invoiceList` is empty, see [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – trincot Aug 02 '17 at 12:25
  • @trincot it must be something like that - cause when the DB is slow it suddenly works and then I reload and then it does not work. You think the console.log result is "lying" (appearing later, after the result has come?) – torbenrudgaard Aug 02 '17 at 12:31
  • 1
    See also: https://stackoverflow.com/questions/13218273/when-does-console-log-get-executed – jsalonen Aug 02 '17 at 12:32
  • `console.log` is not lying, it is designed to work like that. Did you try `JSON.stringify` like I suggested? – trincot Aug 02 '17 at 12:36
  • @trincot yes I tried it, shows the same result. - Im gonna try changing all variable names - usually when you got ghost-in-the-machine errors its because of some conflict somewhere :-) – torbenrudgaard Aug 02 '17 at 18:36
  • @jsalonen very interesting post and gives some kind of explanation, but if we cannot do `var x=5; if (!x) {do something}` in javascript then what can we do? heheh :-D – torbenrudgaard Aug 02 '17 at 18:40
  • 2
    Without enough code to reproduce the issue we cannot do much for you. – trincot Aug 02 '17 at 18:41
  • @trincot I changed the name from invoiceList to invList - now everything works perfectly. I cant find any use of invoiceList in my code, but i do have a lot of includes and directives/facories etc.. So perhaps somewhere there is a conflict of some sort. Anyway thanks for trying to help :) – torbenrudgaard Aug 02 '17 at 19:19

0 Answers0