Node.js 无法使用“;未定义”;作为Firestore的价值
我试图使用node.js从Firestore数据库检索数据,我想从一个Firestore查询中收集一个字段并将值传递到另一个Firestore查询,但我的日志中不断出现此错误,第一个Firestore查询成功检索数据,但我的问题是将值传递到第二个查询Node.js 无法使用“;未定义”;作为Firestore的价值,node.js,firebase,google-cloud-firestore,Node.js,Firebase,Google Cloud Firestore,我试图使用node.js从Firestore数据库检索数据,我想从一个Firestore查询中收集一个字段并将值传递到另一个Firestore查询,但我的日志中不断出现此错误,第一个Firestore查询成功检索数据,但我的问题是将值传递到第二个查询 Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore
Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore value. If you want to ignore undefined values, enable `ignoreUndefinedProperties`. at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:271:19) at validateQueryValue (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:2048:18) at CollectionReference.where (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:988:9) at step2 (/workspace/index.js:74:43) at /workspace/index.js:65:17 at QuerySnapshot.forEach (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:748:22) at updateBets (/workspace/index.js:60:22) at processTicksAndRejections (internal/process/task_queues.js:97:5)
这是我的密码
async function updateBets() {
var marketRef = db.collection('matches');
var snapshot = await marketRef.where('matchStatus', '==', 'FINISHED').get();
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
console.log('I found documents');
snapshot.forEach(doc => {
step2();
async function step2() {
var marketRef2 = db.collection('markets');
var snapshot2 = await marketRef2.where('marketId', '==', doc.data().matchId).get();
console.log(doc2.id, '=>', doc2.data());
snapshot2.forEach(doc2 => {
console.log(doc2.id, '=>', doc2.data());
if (doc2.data().marketTitleId == 'FULL_TIME_RESULT') {
var a = doc.data().homeTeamScore;
var b = doc.data().awayTeamScore;
var winnerIndex;
if (a > b) {
winnerIndex = 0;
var resultIndex = ['WINNER', 'LOSER', 'LOSER'];
var docName = `${doc.data().matchId}` + '000' + '1';
var sfRef = db.collection('markets').doc(docName);
batch5.update(sfRef, {
results: resultIndex
});
} else if (a == b) {
winnerIndex = 1;
var docName = `${doc.data().matchId}` + '000' + '1';
var resultIndex = ['LOSER', 'WINNER', 'LOSER'];
var sfRef = db.collection('markets').doc(docName);
batch5.update(sfRef, {
results: resultIndex
});
} else if (a < b) {
winnerIndex = 2;
var docName = `${doc.data().matchId}` + '000' + '1';
var resultIndex = ['LOSER', 'LOSER', 'WINNER'];
var sfRef = db.collection('markets').doc(docName);
batch5.update(sfRef, {
results: resultIndex
});
}
}
})
}
});
batch5.commit().then(() => {
console.log("im done with results");
}).catch((err) => {
console.log('Mac! there was an error with results: ', err);
});
}
async函数updateBets(){
var marketRef=db.collection('matches');
var snapshot=wait marketRef.where('matchStatus','=','FINISHED').get();
if(snapshot.empty){
console.log('没有匹配的文档');
返回;
}
log(“我找到了文档”);
snapshot.forEach(doc=>{
步骤2();
异步函数step2(){
var marketRef2=db.collection(“市场”);
var snapshot2=wait marketRef2.where('marketId','==',doc.data().matchId).get();
log(doc2.id,'=>',doc2.data());
快照2.forEach(doc2=>{
log(doc2.id,'=>',doc2.data());
如果(doc2.data().marketTitleId=='FULL\u TIME\u RESULT'){
var a=doc.data().homeTeamScore;
var b=doc.data().awayTeamScore;
var winnerIndex;
如果(a>b){
winnerIndex=0;
var resultIndex=[“赢家”、“输家”、“输家”];
var docName=`${doc.data().matchId}`+'000'+'1';
var sfRef=db.collection('markets').doc(docName);
批次5.更新(sfRef{
结果:resultIndex
});
}如果(a==b),则为else{
winnerIndex=1;
var docName=`${doc.data().matchId}`+'000'+'1';
var resultIndex=[“失败者”、“赢家”、“失败者”];
var sfRef=db.collection('markets').doc(docName);
批次5.更新(sfRef{
结果:resultIndex
});
}否则如果(a{
console.log(“我处理完结果”);
}).catch((错误)=>{
console.log('Mac!结果出错:',err);
});
}
您可以尝试:
const data=doc.data();
常量matchId=data.matchId;
然后将matchId放入查询中
还要记录“matchId”变量以查看值。请编辑帖子以删除多余的垂直空白?您已经发布了超过225行代码,其中大约2/3是空白的。这使得这篇文章更难阅读。我刚刚编辑过,请重新检查,你有没有调试过这段代码?我猜,
doc.data().matchId
至少有一个文档没有定义,但您应该检查一下。调试后,我看到代码在其他文档上成功运行。您说的“其他”文档是什么意思?这是否意味着您找到的文档没有匹配ID
?