jimijon
jimijon

Reputation: 2166

Unhandled promise rejection firestore

I don't quite have the promises down I make. Anyway, I am reading a list of rows in a mysql table, then upon writing into Firestore I want to update a count of a "related" record. Basically aggregate counts.

Here is the start:

request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = JSON.parse(body);
            //console.log(data);

            var allObjects = data.resource;
            var obj = {};


            for (j = 0; j < allObjects.length; j++) {
                var records = [];
                var record = allObjects[j];

                //FireStore
                var db = admin.firestore();
                var docRef = db.collection(toCollection).doc(record.nfCode);
                var rec = docRef.set(record);

                addCounts(docRef);


            }

            res.render('index', {title: 'Load Table'});

            offset = offset + limit

        } else {
            return res.send(body);//
            //res.render('index', { title: 'Error' });
        }
    });

Then here is addCount:

    function addCounts(docRef) {

    // In a transaction, update the aggregate totals
    var db = admin.firestore();

    return db.runTransaction(transaction => {
        transaction.get(docRef).then(res => {

        var brandRef = db.collection('brands').doc(res.data().brandNFCode);
    var transaction = db.runTransaction(t => {
        return t.get(brandRef)
            .then(doc => {
            if (res.data().glutenFreeYN == "Y") {

        var glutenFreeCount = doc.data().glutenFreeCount + 1;

        var setWithOptions = transaction.set(brand, {
            glutenFreeProductCount: glutenFreeProductCount

        }, { merge: true });


        return setWithOptions;

    }
});
})
.then(result => {
        console.log('Transaction success', result);
        // return nil
})
.catch(err => {
        // console.log('Transaction failure:', err);
        return nil
});

})
})
}

Is there a better way to do this? And where is my error coming from?

Auth error:Error: socket hang up
(node:90050) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Getting metadata from plugin failed with error: socket hang up

Upvotes: 0

Views: 1465

Answers (2)

Sam Spade
Sam Spade

Reputation: 1496

Was running into this issue and spent over an hour trying to figure it out. Make sure your system time is synced properly. My time was correct, but my time zone wasn't so the system UTC time was out of sync.

To correct on Windows 10, go to Settings -> Time & Language -> Date and Time -> Sync Now

Upvotes: 1

Sam Stern
Sam Stern

Reputation: 25134

There are a few things wrong with your addCounts function that may be causing this:

function addCounts(docRef) {

    // In a transaction, update the aggregate totals
    var db = admin.firestore();

    return db.runTransaction(transaction => {
        return transaction.get(docRef).then(res => {

                        if(res.data().glutenFreeYN == "Y"){
                            var glutenFreeProductCount = res.data().glutenFreeProductCount + 1;

                        }
                        if(res.data().vegetarianYN == "Y"){
                            var vegetarianProductCount = res.data().vegetarianProductCount + 1;

                        }
                        if(res.data().dairyFreeYN == "Y"){
                            var dairyFreeProductCount = res.data().dairyFreeProductCount + 1;

                        }
                        if(res.data().organicYN == "Y"){
                            var organicProductCount = res.data().organicProductCount + 1;

                        }


                        // LOOK HERE!!! You had brand.set()
                        // this version uses the transaction
                        var setWithOptions = transaction.set(brand, {
                            glutenFreeProductCount: glutenFreeProductCount,
                            organicProductCount: organicProductCount,
                            vegetarianProductCount: vegetarianProductCount,
                            dairyFreeProductCount: dairyFreeProductCount

                        }, { merge: true });

                        // LOOK HERE!! Make sure to return the 
                        // set operation
                        return setWithOptions;

                });

                // I removed the .catch() here as it will obscure 
                // errors. You can catch the result of addCounts.
    })
};

Upvotes: 1

Related Questions