#smilelari-Invoices

1 messages · Page 1 of 1 (latest)

open bay
#

Hi there, can you share with me the code?

radiant scaffold
#

async function getLinesForInvoice(invoiceId: string, lastLineId: string | null): Promise<any> {
try {
const params = {}

    const invoiceLinesCollection = []
    if (lastLineId !== null) {
        params["starting_after"] = lastLineId
    }

    console.log(invoiceId)

    for await (const line of stripe.invoices.listLineItems(invoiceId, params)) {

        lastLineId = line.id

        // do something with line
        console.log(line.id)

        const invoiceLinesCollectionObject = {
            invoice_id: invoiceId,
            id: line.id,
            amount: line.amount,
            period_end: line.period?.end,
            period_start: line.period?.start,
            // more properties
        }

        invoiceLinesCollection.push(invoiceLinesCollectionObject)
    }
    return invoiceLinesCollection
}
catch(err) {
    setTimeout(getLinesForInvoice, 1000, invoiceId, lastLineId)
}

}

open bay
#

So you have 13 line items in this invoice

#

I don't think the API will return you 1000+ items (Stripe doesn't even allow that much items), what make you think it returns 1000+ items?

radiant scaffold
#

That's the size of the collection when it's done. Must be something in my code. Thanks

#

I'm stepping through the for await and outputting line ids and I'm definitely seeing duplicates. Any idea why I might be multiples of the same ID?

#

in_1KAliUEwYEdSC6hP6rjyCsEP
ii_1KAltrEwYEdSC6hPWsMct611
ii_1K2g3UEwYEdSC6hPiErQ1nFf
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW
ii_1K2eQTEwYEdSC6hPfuoiwxvK
ii_1K2eQTEwYEdSC6hPHaZ7wxIE
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW

open bay
#

These IDs look unique to me.

radiant scaffold
#

ii_1KAltrEwYEdSC6hPWsMct611
ii_1K2g3UEwYEdSC6hPiErQ1nFf
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW
ii_1K2eQTEwYEdSC6hPfuoiwxvK
ii_1K2eQTEwYEdSC6hPHaZ7wxIE
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW

#

I highlighted 2 that are not

#

There are a couple other duplicates

open bay
#

I see, is it possible that getLinesForInvoice function was called more than once?

#

and is lastLineId != null?

radiant scaffold
#

lastLineId is null and it is only being called once

open bay
#

because I see you are setting the lastLine here lastLineId = line.id

radiant scaffold
#

It's null when it's called initially. It's only populated if it lands in the catch and retries

#

I don't see this with other invoices

open bay
#

I'll suggest to to simply remove the params here, I don't see a reason why you want to set starting_after in an auto-pagination.

radiant scaffold
#

ok

#

I was having periodic issues where an exception would be thrown while retrieving line items StripeConnectionError - and was trying to come up with a way to retry in that event

#

Or I guess continue where it left off

#

Still seeing the same behavior without the params

open bay
#

OK, can you send me the updated code?

radiant scaffold
#

async function getLinesForInvoice(): Promise<any> {
try {

    const invoiceId = 'in_1KAliUEwYEdSC6hP6rjyCsEP'

    const invoiceLinesCollection = []
    console.log(invoiceId)

    for await (const line of stripe.invoices.listLineItems(invoiceId)) {
        // do something with line
        console.log(line.id)

        const invoiceLinesCollectionObject = {
            invoice_id: invoiceId,
            id: line.id,
            amount: line.amount,
            period_end: line.period?.end,
            period_start: line.period?.start
        }

        invoiceLinesCollection.push(invoiceLinesCollectionObject)
    }
    return invoiceLinesCollection
}
catch(err) {
    console.log(err)
}

}

open bay
radiant scaffold
#

I think the typescript wasn't transpiled. I'm still seeing duplicates. Looking at the logs it looks like multiple requests, which I assume is wrong

#

I also got a StripeConnectionError

#

type: 'StripeConnectionError',
raw: {
message: 'An error occurred with our connection to Stripe.',
detail: Error: write ECONNABORTED

open bay
#

can you send me the full error details?

radiant scaffold
#

StripeConnectionError: An error occurred with our connection to Stripe.
at C:\Dev\saige-cli\node_modules\stripe\lib\StripeResource.js:519:15
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
type: 'StripeConnectionError',
raw: {
message: 'An error occurred with our connection to Stripe.',
detail: Error: write ECONNABORTED
at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:94:16)
at WriteWrap.callbackTrampoline (internal/async_hooks.js:131:17)
at writevGeneric (internal/stream_base_commons.js:134:26)
at TLSSocket.Socket._writeGeneric (net.js:785:11)
at TLSSocket.Socket._writev (net.js:794:8)
at doWrite (internal/streams/writable.js:375:12)
at clearBuffer (internal/streams/writable.js:521:5)
at TLSSocket.Writable.uncork (internal/streams/writable.js:317:7)
at ClientRequest.end (_http_outgoing.js:855:17)
at ClientRequest.<anonymous> (C:\Dev\saige-cli\node_modules\stripe\lib\net\NodeHttpClient.js:79:15) {
errno: -4079,
code: 'ECONNABORTED',
syscall: 'write'
}
},

#

I can't paste the whole thing, but it's attached

open bay
#

Seems like you are having a connection error.

radiant scaffold
#

So, I also see the starting_after in the request, however, I am not passing it anywhere. I notice that the id that is being passed is actually an ID for a subscription.

open bay
#

Hmm, maybe you want do some debugging of your code then.

radiant scaffold
#

This request has returned 250 records so far

#

Is there a better way to make this request? I need to retrieve all line items for an invoice

open bay
radiant scaffold
#

const allLines = await stripe.invoices.listLineItems(invoiceId).autoPagingToArray({limit: 10000})

#

Is this correct?

open bay
#

Yup it's correct

radiant scaffold
#

That call returned all 10000 requested lines. I have no idea why. But I will look at it more tomorrow