bitpay / node-bitpay-client

A Node.js module and command line client for interacting with BitPay's Cryptographically Secure API
102 stars 95 forks source link

EventParser error at using event bus #106

Open hrabkin opened 4 years ago

hrabkin commented 4 years ago

I see this error at trying to use an event bus

merchant facade

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at /Users/zestu/Finance/BitPayService/node_modules/bitpay-rest/lib/event-parser.js:35:26
    at Array.forEach (<anonymous>)
    at /Users/zestu/Finance/BitPayService/node_modules/bitpay-rest/lib/event-parser.js:27:11
    at Array.forEach (<anonymous>)
    at EventParser._transform (/Users/zestu/Finance/BitPayService/node_modules/bitpay-rest/lib/event-parser.js:22:12)
    at EventParser.Transform._read (_stream_transform.js:190:10)
    at EventParser.Transform._write (_stream_transform.js:178:12)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)

Here's the code I use:

app.get("/create_bitpay_invoice", (req, res, next) => {

    client.as('merchant').post('invoices', data, function(err, invoice) {

        if (err) {
            // more error handling
            return console.log(err);
        } else {
            // success
            // get event bus token
            client.as('merchant').get(invoice.path + 'events', function(err, buspass) {
                if (err) console.log(err);

                let data = {
                    token: buspass.token,
                    action:'subscribe',
                    events: ['payment']
                };
                // pipe event stream request through bitpay.EventParser
                var events = client.as('merchant').get('events', data);
                var parser = new bitpay.EventParser();

                events.pipe(parser).on('payment', function(invoice) {

                    console.log('invoice is ' + invoice.status);
                });

                res.json({msg: invoice});
            });
        }
    });
});