简体   繁体   English

在AsynData Nuxt上运行Firebase

[英]Run Firebase on AsynData Nuxt

My problem is the firebase request on asyncData method on Nuxt. 我的问题是Nuxt上asyncData方法的firebase请求。 When the app is on the client the request works but the first load doesn´t work. 当应用程序在客户端上时,请求有效,但第一次加载无效。 So, on the server, have i install anything? 因此,在服务器上,我是否安装了任何东西?

This is my code: 这是我的代码:

<script>
 import { db } from '~/plugins/firebase.js'
 export default {
   asyncData (context) {
     let listUsers = []
      db.collection('users').get()
       .then(doc => {
          doc.forEach(user => {
            listUsers.push({ id: user.id, ...user.data() })
          });
       })

    return {
      dataUsers: listUsers  
    }
  }
}
</script>

The problem I see as @Andrew1325 noted, is that you do NOT return a promise from asyncData . @ Andrew1325指出的问题是,您没有从asyncData返回诺言。 This means that the server will NOT wait for the request to finish before sending HTML to the client. 这意味着服务器在将HTML发送给客户端之前不会等待请求完成。

On the other hand, how do you access your dataUsers in the component? 另一方面,如何访问组件中的dataUsers

I suggest you to reformat the code to dispatch an action 我建议您重新格式化代码以调度action

<script>
 import { db } from '~/plugins/firebase.js'
 export default {
   asyncData ({store}) {
     return store.dispatch('FETCH_USERS')
  }
}
</script>

And the a simple action 而一个简单的动作

async function FETCH_USERS = ({commit}) => {
    const doc = await db.collection('users').get()
    commit('SET_USERS', doc)
}

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

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