我没有完全履行承诺。无论如何,我正在读取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
答案 0 :(得分: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.
})
};
答案 1 :(得分:1)
遇到此问题并花了一个多小时试图解决。确保系统时间正确同步。我的时间是正确的,但是我的时区不正确,因此系统UTC时间不同步。
要在Windows 10上更正,请转到“设置”->“时间和语言”->“日期和时间”->“立即同步”