简体   繁体   English

如何将数据从 Firestore 导入 Vue CLI 项目?

[英]How can I import data from Firestore to a Vue CLI project?

I am trying to import data from a Firestore Database into a Vue CLI project but can not get it to work.我正在尝试将 Firestore 数据库中的数据导入 Vue CLI 项目,但无法使其正常工作。 I have followed several tutorials and my version never works.我遵循了几个教程,但我的版本从来没有用过。 It seems I have a problem retrieving the data each time I try, as my console.log shows nothing.每次尝试检索数据时似乎都有问题,因为我的 console.log 没有显示任何内容。

I have the following in a JS file (index.js which lives in a folder called db - Plus for security, I have removed the content in quotations for here on Stackoverflow);我在一个 JS 文件中有以下内容(index.js 位于一个名为 db 的文件夹中 - 为了安全起见,我已在 Stackoverflow 上删除了引用中的内容);

import firebase from "firebase";
import "firebase/firestore";

var config = {
  apiKey: "",
  authDomain: "",
  databaseURL: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};
firebase.initializeApp(config);
const db = firebase.firestore();
db.settings({ timestampsInSnapshotps: true });

Then in my component I have the below;然后在我的组件中,我有以下内容;

import db from '@/db'

export default {
  name: 'HelloWorld',
  data () {
    return {
        cafes: []
    }
  },
  created () {
      db.collection('cafes').get().then((snapshot) => {
        console.log(snapshot.docs);
      });
  }
}

I have read that I nolonger need the db.settings({ timestampsInSnapshotps: true });我已经读到我不再需要db.settings({ timestampsInSnapshotps: true }); However, when I remove this it errors in the terminal and browser.但是,当我删除它时,它会在终端和浏览器中出错。

Template as below;模板如下;

<template>
  <div class="cafes">

    <h1>Here are some cafés</h1>

    <div for="cafe in cafes" v-bind:key="cafe.name">
        <div>
            {{cafe.name}}
        </div>
    </div>

  </div>
</template>

Any help welcome as I have been trying for days.欢迎任何帮助,因为我已经尝试了几天。

Do as follows:执行以下操作:

Adapt your Firebase db/index.js as follows:调整您的 Firebase db/index.js如下:

import firebase from 'firebase'
import 'firebase/firestore'

// firebase init goes here
const config = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: ""
}
firebase.initializeApp(config)

const db = firebase.firestore()

// firebase collections
const cafesCollection = db.collection('cafes')

export {
    db,
    cafesCollection
}

Adapt your Component as follows:如下调整您的组件:

const firebase = require("../db/index.js");  // Adapt the path as desired

export default {
  name: 'HelloWorld',
  data () {
    return {
        cafes: []
    }
  },
  created () {
      firebase.cafesCollection.get().then((snapshot) => {
        console.log(snapshot.docs);
        let cafesArray = [];
        snapshot.forEach(doc => {
          cafesArray.push({id: doc.id, ...doc.data()});
        });
        this.cafes = cafesArray;
      })
      .catch(error => {
         console.log(error);
      })
  }
}

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

相关问题 如何从Vue JS项目的文件夹中导入JS文件? - How can I import a JS file from a folder in my Vue JS project? 在使用 Vue CLI 项目设置时,如何将 $emit 与 Vue.js 一起使用? - How can I use $emit with Vue.js when using the Vue CLI project setup? 使用 CLI 创建新项目后如何在 vue 3 中正确导入 Axios? - How to correctly import Axios in vue 3 after creating new project with CLI? 如何将来自服务器的数据注入 Vue CLI 应用程序? - How do I inject data from the server into a Vue CLI app? Firestore:发布项目后如何更新数据模型? - Firestore: How can I update a data model after releasing a project? 如何在Angular 5(CLI)项目中导入和使用纯JavaScript文件的功能 - How can I import and use the functionality of a plain JavaScript file in my Angular 5 (CLI) project 如何挂载像 codepen 这样的 Vue 3 CLI 项目 - How do I mount a Vue 3 CLI project like codepen vue-chartjs 无法从 vue 导入数据 - vue-chartjs can't import data from vue 如何将数据从 firestore 设置为全局变量 - How can I set data from firestore to a global variable 如何在计划 function 中从 Firestore 数据库中读取数据? - How can I read data from firestore database in a schedule function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM