简体   繁体   English

如何在 Vuex4 中正确使用 firebase firestore 的 onSnapshot?

[英]How to correctly way to use onSnapshot of firebase firestore in Vuex4?

I try to use firebase firestore in my latest vue3.js webapps.我尝试在我最新的 vue3.js webapps 中使用 firebase firestore。 I have used vuex4 as sate management in my apps.我在我的应用程序中使用 vuex4 作为状态管理。 My apps also use firebase authentication as login, logout, and make new users.我的应用程序还使用 firebase 身份验证作为登录、注销和创建新用户。

My apps use snapShot function after login, but an error occurred like below.我的应用程序在登录后使用snapShot function,但出现如下错误。

Uncaught Error in snapshot listener: FirebaseError: Missing or insufficient permissions.

But I have made the code unsubscribe onSnapshot function after logout event.但是我已经在注销事件后取消订阅onSnapshot function 的代码。 My code is like below:我的代码如下:

store/index.js商店/index.js

const store = createStore({
 state: {
  unsubscribe: null,
 },
 actions: {
  startListner(context) {
   context.state.unsubscribe = onSnapshot(
      //something result get proccess
   )
  },
  stopPostsListner(context) {
      context.state.unsubscribe();
    },
 }
})

I made onSnapshot function as vuex actions methods.我将onSnapshot function 作为 vuex 操作方法。 And then use these functions at index.vue.然后在 index.vue 中使用这些函数。

index.vue索引.vue

<script setup>
import { useStore } from "vuex";
import { onMounted, onUnmounted } from "vue";
const store = useStore();

onMounted(() => {
  store.dispatch("startPostsListner");
});
onUnmounted(() => {
  store.dispatch("stopPostsListner");
});
</script>

I thought onSnapshot function unsubscribe after destroying the component, but errors keep occurring.我以为onSnapshot function 销毁组件后取消订阅,但错误不断。

How correctly way to use onSnapshot in vuex4?在 vuex4 中如何正确使用onSnapshot

Posting my comment as an answer for better visibility.发表我的评论作为答案以获得更好的可见性。

From the security rules by changing:通过更改安全规则:

allow read, write: if

from false;false; to true;true; will help to fix the error.将有助于修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM