You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

问题原因

  1. where方法参数格式错误:Firestore的where方法要求传入三个参数(字段名、比较操作符、匹配值),直接传入对象不符合API规范,导致内部处理时无法正确读取路径,触发错误。
  2. 冗余的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:31:11