[英]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.