Node.js+Firestore筛选数据报错:Cannot read properties of undefined (reading '_internalPath')
解决Firestore数据筛选时的"Cannot read properties of undefined (reading '_internalPath')"错误
错误场景
在Node.js中使用Firestore进行数据筛选时,Postman返回以下错误:
"Cannot read properties of undefined (reading '_internalPath')"
原代码
const getAllWords = async (req, res, next) => { let categoryName = "fruits" try { const word = await firestore.collection('12words'); const data = await word.where({categoryName}).get(); const wordArray = []; if(data.empty) { res.status(404).send('No words12 found'); }else { data.forEach(doc => { const words = new Word12( doc.id, doc.data().name, doc.data().categoryName, ); wordArray.push(words); }); res.send(wordArray); } } catch (error) { res.status(400).send(error.message); } }
问题原因
where方法参数格式错误:Firestore的where方法要求传入三个参数(字段名、比较操作符、匹配值),直接传入对象不符合API规范,导致内部处理时无法正确读取路径,触发错误。- 冗余的
await:firestore.collection('12words')是同步获取集合引用的操作,无需使用await。
修正后的代码
const getAllWords = async (req, res, next) => { let categoryName = "fruits" try { const wordCollection = firestore.collection('12words'); const data = await wordCollection.where('categoryName', '==', categoryName).get(); const wordArray = []; if(data.empty) { res.status(404).send('No words12 found'); }else { data.forEach(doc => { const words = new Word12( doc.id, doc.data().name, doc.data().categoryName ); wordArray.push(words); }); res.send(wordArray); } } catch (error) { res.status(400).send(error.message); } }
关键修改点
- 移除
firestore.collection('12words')前的await,同步获取集合引用即可。 - 将
word.where({categoryName})替换为wordCollection.where('categoryName', '==', categoryName),严格遵循Firestore的where方法参数要求。
内容的提问来源于stack exchange,提问作者Dasun Dola
相关产品推荐
相关产品推荐

