【问题标题】:FirebaseError: Function CollectionReference.doc() cannot be called with an empty pathFirebaseError:无法使用空路径调用函数 CollectionReference.doc()
【发布时间】:2021-03-25 04:52:17
【问题描述】:

我正在从用户集合中获取 id 列表,然后当我尝试从另一个集合中获取 id 数据时出现此错误:

FirebaseError:无法使用空路径调用函数 CollectionReference.doc()

<script>
import { db } from "../Utils/fire.js"
import { current_user } from "../Utils/auth.js"
import Room from "./Room.svelte"

async function interestedForUser(){
    let query = await db.collection("users").doc($current_user.uid).get()
    const listingsIds = await query.data().anunturi_interesat 
    console.log(listingsIds) //ok
    let anunturi = []
    for (let id of listingsIds) {
        console.log(id, typeof(id)) // ok
        let anunt = await db.collection("anunturi").doc(id).get() // nok
        let anunt_data = await anunt.data() 
        if (anunt_data) {
            anunturi.push({...anunt_data, listingId:id})
        } 
    }
    return anunturi
}

</script>

{#await interestedForUser()}
    <p class="text-center mt-12">Se incarca...</p>
{:then listings}
    {console.log("In template:", listings, listings.length)} //ok (but why?)
    {#if listings.length > 0} // this doesn't get rendered..
        {#each listings as camera }
            <Room {camera}/>
        {/each}
    {:else}
        <p class="text-center mt-12">Nu ai postat nici un anunt</p>
    {/if}
{:catch error}
    <p style="color: red">{error.message}</p>
{/await}

更新:

问题出在&lt;Room {camera}/&gt; 组件中。 Room 组件的子组件具有未定义的 firestore 引用。

【问题讨论】:

    标签: javascript google-cloud-firestore svelte-3


    【解决方案1】:

    错误信息告诉你,这段代码有问题:

    doc(String(id))
    

    如果id 已经是一个字符串(我们无法从您这里显示的内容中看出),那么直接传递它:

    doc(id)
    

    如果它是一个数字,并且你想将它转换为一个字符串:

    doc(id.toString())
    

    如果是其他内容,那么您必须更具体地说明您希望如何将其转换为您希望 Firestore 使用的文档 ID 的字符串。

    【讨论】:

    • id 由 firestore 自动生成。无论有没有String(id),我都会遇到同样的错误(我在另一篇SO帖子中看到id必须是字符串)。这很奇怪,因为对于 collection("users").doc($current_user.uid) 它有效。
    • 我添加了所有代码,删除了String(id)。通过 ref 获取数据在您帮助我 (so post) 的相同组件中工作。可能与苗条有关。
    • 尝试在 interestedForUser 的 for 循环之前检查 listingsIds。像if listingsIds 然后运行循环,否则返回一个空数组
    • 我将 for 循环包裹在 if (listingsIds.length &gt; 0) 中,但得到了相同的结果。
    【解决方案2】:

    当我尝试从 Firebase 获取特定 id 的数据时,我在 Next JS 中遇到了相同类型的问题。

    如果你在下一个 js 中遇到问题,试试这个。

    import React, { useState ,useEffect} from "react";
    import StudentsWithID from "../../Componenets/Navbar/Form/StudentsWithID";
    import firebase from 'firebase/app';
    
    const StudentWithId = ({sid}) => {
      const [student,setStudent] =useState();
      /*
      const router = useRouter();
      const { sid } = router.query; 
      
      ^^^^^^^^^^^^^^^^^^^^^
      iiiiiiiiiiiiiiiiiiiii
      
      I try to get id of student with these lines. but It doesn't when i build the project (npm run build)
      ?: file name is [sid].js || It is not working in [id].js
    
      */
      useEffect(()=>{
        const loadStudent=async ()=>{
       try{
          const result= await firebase.firestore().collection("students").doc(String(""+sid.sid)).get()
          const my_student=result.data()
          if(my_student){
          setStudent(my_student)
          console.log(my_student)
        }else{
          console.log("Stundent not found")
        }
      }catch(err){
    console.log("-----------------StudentError------------------")
    console.log(err.message);
      }}
    loadStudent()
      },[])
     
      return (
        <div>
          <StudentsWithID props={student}/>
        </div>
      );
    };
    
    export async function getServerSideProps({query,params}) {
      //todo: when I try to get id in server it works perfectly.
    const sid=query||params
      return{
        props:{
          sid:sid
        }
      }
    
    }
    
    export default StudentWithId;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-20
      • 2021-08-29
      • 2020-07-13
      • 2023-03-24
      • 2012-10-22
      • 2020-12-06
      • 2019-10-18
      • 2010-10-18
      相关资源
      最近更新 更多