【发布时间】:2022-01-22 04:13:59
【问题描述】:
美好的一天。 拜托,我需要以下关于我的 vue + firebase 代码的帮助。我可以成功上传用户详细信息和图像,但我很难在我的 vue 组件上显示图像。这些值在 Devtools 中返回为未定义。请帮帮我...
methods: {
async storeCourse() {
try {
this.isLoading = false
// upload file
const fileRef = 'uploads/courses/' + this.file.name
const snapshot = await storage.ref(fileRef).put(this.file)
let data = {
userId: auth.currentUser.uid,
subject: this.subject,
description: this.description,
tutor: this.tutor,
payment: this.payment,
duration: this.duration,
amount: this.amount,
years_exp: this.years_exp,
image: fileRef
}
window.alert('Added!')
const docRef = coursesCollection.doc();
data.id = docRef.id;
const doc = await docRef.set(data);
// const doc = await coursesCollection.add(data)
this.$emit('course:added', data)
await this.resetForm()
this.isLoading = false
this.dialog = false
} catch(e) {
console.log(e)
}
},
async getUser() {
try {
// const querySnapshot = await coursesCollection.where('userId', '==', auth.currentUser.uid).get()
const querySnapshot = await usersCollection.where('userId', '==', auth.currentUser.uid).get()
querySnapshot.forEach( async (doc) => {
let img = ''
if(doc.data().id_upload) {
img = await storage.ref().child(doc.data().id_upload).getDownloadURL()
}
this.profile.push({
id_upload: img,
img: doc.data().id_upload
})
})
} catch(e) {
console.log(e)
}
},
},
async mounted() {
await this.getGames()
await this.getUser()
}
我正在尝试通过以下方式访问数据:
<h1>Hi, {{profile.name}} </h1>
<v-img :src="profile.id_upload"></v-img>
我已将我的 getUser() 更改为下面的代码,它说记录未找到,而记录正常存在。
async getUser() {
let ref = usersCollection.where('userId', '==', auth.currentUser.uid).get()
.then(snapshot => { //DocSnapshot
if (snapshot.exists) {
let profile = snapshot.data()
} else {
// snapshot.data() will be undefined in this case
console.log("No such document!");
}
})
},
请问我哪里漏掉了?
【问题讨论】:
标签: firebase vue.js google-cloud-firestore