【问题标题】:Error: Property or method "users" is not defined on the instance but referenced during render错误:属性或方法“用户”未在实例上定义,但在渲染期间被引用
【发布时间】:2020-05-11 12:05:31
【问题描述】:

我在显示存储在 Firebase 数据库集合中的数据时遇到问题。 最初我收到错误 “Function CollectionReference.doc() 要求其第一个参数的类型为非空字符串,但它是:未定义” 我可以通过在路由器配置文件中提供正确的路径来解决这个问题,但现在我收到了错误: 属性或方法“用户”未在实例上定义,但在渲染期间引用

代码

    <template>
    <div class="profile container">
        <h2 class="deep-purple-text center">Your information</h2>
                <div class="card-content">
                    <ul class="info">
                        <li v-for="(user, index) in users" :key="index">
                            <p>{{ profile.name }}</p>
                        </li>
                        <li v-for="(user, index) in users" :key="index">
                            <p>{{ profile.address }}</p>
                        </li>
                        <li v-for="(user, index) in users" :key="index">
                            <p>{{ profile.zip }}</p>
                        </li>
                        <li v-for="(user, index) in users" :key="index">
                            <p>{{ profile.phone }}</p>
                        </li>
                    </ul>
                </div>
        </div>
    </template>
    <script>
    import db from '@/firebase/init'
    import firebase from 'firebase'
    export default {
        data(){
            return{
                profile: null,


            }
        },
 created(){
    let user = firebase.auth().currentUser 
    db.firestore().collection('users').where('user_id', '==', user.uid).get()
    .then(snapshot => {
        snapshot.forEach((doc) => {
                this.profile = doc.data()
            })
        })
      }
    }
    </script>

这是另一个用户要求的我的路由器配置。

import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/Index'
import AddProduct from '@/components/AddProduct'
import EditProduct from '@/components/EditProduct'
import Signup from '@/components/Signup'
import Login from '@/components/Login'
import Userinfo from '@/components/Userinfo'
import firebase from 'firebase'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index,
    },
    {
      path: '/add-product',
      name: 'AddProduct',
      component: AddProduct,
    },
    {
      path: '/edit-product/:product_slug',
      name: 'EditProduct',
      component: EditProduct,
    },
    {
      path: '/signup',
      name: 'Signup',
      component: Signup
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/profile',
      name: 'Userinfo',
      component: Userinfo,
    }
  ]
})

router.beforeEach((to, from, next) => {
  if(to.matched.some(rec => rec.meta.requiresAuth)) {
    let user = firebase.auth().currentUser
    if(user){
      next()
    } else{
      next({ name: 'Login' })
    }
  } else{
    next()
  }
})
export default router

【问题讨论】:

  • 看来this.$route.params.id是未定义的。
  • 我应该如何以及在哪里定义它?
  • 通常情况下,如果您已经正确配置了您的路由器,它来自用于调用应用程序页面的路径。您能否在问题中添加您的路由器配置(通过上面的编辑链接编辑问题)?
  • 我添加了路由器配置。谢谢=)
  • 我还编辑了“创建”部分。它仍然不起作用,但至少我认为我在正确的轨道上。

标签: javascript firebase vue.js google-cloud-firestore


【解决方案1】:

更正路由器配置问题后,您现在遇到以下错误:

属性或方法“用户”未在实例上定义,但 在渲染期间引用

这是因为,在模板中您引用了users,但该属性未在脚本的data 对象中声明。

你应该这样做:

export default {
 data(){
     return{
        users: []
     }
 },
 created(){
    let user = firebase.auth().currentUser 
    db.firestore().collection('users').where('user_id', '==', user.uid).get()
    .then(snapshot => {
        let usersArray = [];
        snapshot.forEach((doc) => {
             usersArray.push(doc.data());
        })
        this.users = usersArray;
      });
 }

现在,完整地说,您似乎只想在此页面中显示一位用户的个人资料。所以你可能不应该使用&lt;li v-for="(user, index) in users" :key="index"&gt;,当你循环几个项目时使用它(即users这里)。但这可能应该是另一个问题的主题。

【讨论】:

  • 感谢您的评论。它确实解决了之前的错误消息,但现在我收到了一个新错误:Error in created hook: "TypeError: _WEBPACK_IMPORTED_MODULE_0_firebase_init__.a.firestore is not a function"我们很清楚,在我的 init.js 文件中,我确实记得输入 import * as firebase from 'firebase'; import 'firebase/firestore'; 这应该可以解决这个问题。我也尝试再次运行 Npm Run Dev 以刷新本地主机上的项目,但该错误仍然存​​在。
  • 我建议您提出一个新问题,其中包含 Vue.js/Firebase 配置的所有详细信息。最好将问题/答案对的范围限制在特定问题上。如果您认为它解决了您最初的问题,请接受我的回答,请参阅stackoverflow.com/help/someone-answers
猜你喜欢
  • 2018-09-17
  • 2022-07-14
  • 2020-03-06
  • 2021-07-07
  • 2018-05-07
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
相关资源
最近更新 更多