next.js Firebase Firestore - '无法获取文档,因为客户端离线'

xggvc2p6  于 2023-06-22  发布在  其他
关注(0)|答案(2)|浏览(155)

我正在使用NextJS,并尝试在按下按钮后从组件内的firestore中获取数据。我这样描述我的职责:

const handleSkip = async (e) => {
  e.preventDefault();
  const userDocRef = doc(db, "users", session?.id);

  try {
    const userDocs = await getDoc(userDocRef);

    await updateDoc(userDocRef, {
      city: "TK",
    });
  } catch (error) {
    console.log(error);
  }
};

但是当我这样做时,我在DevTools控制台中收到以下错误:

next-dev.js?3515:32 @firebase/firestore: Firestore (9.4.1): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=not-found]: The project undefined does not exist or it does not contain an active Cloud Datastore or Cloud Firestore database. Please visit http://console.cloud.google.com to create a project or https://console.cloud.google.com/datastore/setup?project=undefined to add a Cloud Datastore or Cloud Firestore database. Note that Cloud Datastore or Cloud Firestore always have an associated App Engine app and this app must not be disabled.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

和/或

FirebaseError: Failed to get document because the client is offline.
    at next (index.esm2017.js?a9d0:14370:1)
    at eval (index.esm2017.js?a9d0:13834:1)

奇怪的是,我还使用了next-auth,firestore数据提取在会话回调函数中工作得很好。有什么办法解决这个问题吗?我已经尝试了Firebase 9.4和9.6,但结果相同。

ikfrs5lh

ikfrs5lh1#

对于那些也遇到这个问题的人,我烧了一个上午四处寻找解决方案,最后在this post about writing to firestore上。
在我的.env文件和app.config.js文件中,我错误地调用了我的Project ID(在调用中省略了一个空格)。检查一下--如果你一直在遵循其他Firestore/React Native指南,你可能复制了我做的相同代码片段,让Auth工作,然后在处理DB时遇到了错误。

g0czyy6m

g0czyy6m2#

如果其他人遇到这个问题,它会让我发疯。但你很幸运我疯了所以你不用这么做。
首先console.log您的firebase配置文件。是的,检查你是否已经把正确的变量名从你的.env文件中复制数据。
这就是我错的地方我不小心把
projectId:process.env.NEXT_PUBIC_FIREBASE_PROJECT_ID,
所以,我没有输入公共,而是输入公共,是的,我知道愚蠢的错误,但没有什么告诉我,我打错了名字。但这就是我解决了问题的原因,似乎对上面的那个家伙也是一样的。

相关问题