使用Nuxt Firebase的where查询报错:FieldPath.assess未定义
Nuxt Firebase Firestore查询报错解决方案
错误根源
你代码里写的FieldPath.assess()是拼写错误——Firestore的FieldPath类根本没有assess这个方法,你应该是想调用FieldPath.documentId()来针对文档ID做筛选查询。
修正后的代码
this.$fire.firestore.collection('lights') .where(this.$fireModule.firestore.FieldPath.documentId(), '==', 'ZxBc9dMxVdodbHtnAf5g') .get() .then(snapshot => { // 这里写处理查询结果的逻辑 snapshot.forEach(doc => { console.log(doc.id, '对应数据:', doc.data()); }); }) .catch(error => { console.error('查询失败:', error); });
额外验证点
- 检查
nuxt.config.js里不仅要启用functions,还要确保Firestore服务已开启:// nuxt.config.js 相关配置示例 modules: [ ['@nuxtjs/firebase', { config: { // 填入你的Firebase项目配置信息 }, services: { firestore: true, // 必须开启此项才能使用Firestore functions: true } }] ] - 如果是在
asyncData或nuxtServerInit这类服务器端/异步钩子中调用,需要通过上下文参数(比如context.$fire、context.$fireModule)来获取Firebase实例,而不是直接用this。
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

