!即使数组中有记录,也会触发array.length

时间:2017-08-02 11:55:26

标签: javascript arrays node.js

我不明白这个。

invoiceList显示一条记录。

LENGTH:显示1

但它仍会触发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;
    }

为什么这样!当length为1时触发invoiceList.length?

我刚试过这个:

    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;

并且console.log给了我以下内容:

[ { 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]

所以现在我真的很困惑?!?!

0 个答案:

没有答案