【问题标题】:Unhandled promise rejection firestore未处理的承诺拒绝 Firestore
【发布时间】:2018-03-31 17:31:57
【问题描述】:

我并没有完全兑现承诺。无论如何,我正在读取 mysql 表中的行列表,然后在写入 Firestore 时,我想更新“相关”记录的计数。基本上是总计数。

这里是开始:

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' });
        }
    });

那么这里是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
});

})
})
}

有没有更好的方法来做到这一点?我的错误来自哪里?

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

【问题讨论】:

  • 你不是.catch()ing。您是在问如何修复未处理的部分或插座挂起的部分吗?实际上,您似乎没有对调用 firestore API 可能返回的 Promises 执行任何操作。
  • 这将成为救命稻草,在某处添加: process.on('unhandledRejection', r => console.log(r));并检查您的日志。
  • 您在事务中调用brand.set(...),但您需要调用transaction.set(brand, ....),否则该集合不是事务的一部分。此外,您应该在最后return setWithOptions 确保事务等待集合。

标签: node.js google-cloud-firestore


【解决方案1】:

您的 addCounts 函数存在一些问题,可能会导致此问题:

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.
    })
};

【讨论】:

  • 好的,我添加了试图获得品牌...繁荣
  • (node:45168) UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:18):错误:引用的事务已过期或不再有效。 (node:45168) UnhandledPromiseRejectionWarning: UnhandledPromiseRejectionWarning: Unhandled Promise reject (rejection id: 19): Error: You must return a Promise in your transaction()-callback.
【解决方案2】:

遇到了这个问题,花了一个多小时试图弄清楚。确保您的系统时间已正确同步。我的时间是正确的,但我的时区不正确,因此系统 UTC 时间不同步。

要在 Windows 10 上更正,请转到设置 -> 时间和语言 -> 日期和时间 -> 立即同步

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2018-04-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多