英文:
Iterating through Firebase Database without Google Firebase Cloud Functions?
问题
以下是您要翻译的内容:
"since my goal to create a userlist i need to iterate through Firebase Realtime Database the individual UIDs that are in /users/. How to accomplish this in Vue3 and Firebase for Web even being able to get the data inside the UIDs for each block i want to display? Thank.
I tried
<script setup>
import { getDatabase, ref as dbref, set, child, get, push } from "firebase/database";
import { getAuth } from "firebase/auth";
import { ref, reactive } from "vue";
import "firebase/database";
const auth = getAuth();
const database = getDatabase();
const dbRef = dbref(getDatabase());
const name = reactive({ value: "" });
const email = reactive({ value: "" });
const story = reactive({ value: "" });
const data5 = reactive({ value: '' });
const target = {
message1: "",
message2: "",
};
const handler3 = {
get(target, prop, receiver) {
if (prop === "message2") {
get(child(dbRef, 'users/')).then((snapshot) => {
if (snapshot.exists()) {
let data4 = (JSON.stringify(snapshot.val()));
data5.value = data4
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
}
return Reflect.get(...arguments);
},
};
const proxy3 = new Proxy(target, handler3);
proxy3.message2;
</script>
<template>{{ data5.value }}
<li v-for="email in data5.value">{{ data5.value }}</li>
</template>
It only repeats the whole database with the individual entries in sequence. Does it work? Else i need to go the steps to activate Cloud Functions and actually pay for the Blaze plan, and learn them. I will do that anyway but i want to also try it first because it is essential to what i try.
It is what i try because everything else seems to fail as a concept.
thx."
如果您有任何其他问题,请随时提出。
英文:
since my goal to create a userlist i need to iterate through Firebase Realtime Database the individual UIDs that are in /users/.
How to accomplish this in Vue3 and Firebase for Web even being able to get the data inside the UIDs for each block i want to display?
Thank.
I tried
<script setup>
import { getDatabase, ref as dbref, set, child, get, push } from "firebase/database";
import { getAuth } from "firebase/auth";
import { ref, reactive } from "vue";
import "firebase/database";
const auth = getAuth();
const database = getDatabase();
const dbRef = dbref(getDatabase());
const name = reactive({ value: "" });
const email = reactive({ value: "" });
const story = reactive({ value: "" });
const data5 = reactive({ value: '' });
const target = {
message1: "",
message2: "",
};
const handler3 = {
get(target, prop, receiver) {
if (prop === "message2") {
get(child(dbRef, 'users/')).then((snapshot) => {
if (snapshot.exists()) {
let data4 = (JSON.stringify(snapshot.val()));
data5.value = data4
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
}
return Reflect.get(...arguments);
},
};
const proxy3 = new Proxy(target, handler3);
proxy3.message2;
</script>
<template>{{ data5.value }}
<li v-for="email in data5.value">{{ data5.value }}</li>
</template>
It only repeats the whole database with the individual entries in sequence.
Does it work? Else i need to go the steps to activate Cloud Functions and actually pay for the Blaze plan, and learn them. I will do that anyway but i want to also try it first because it is essential to what i try.
It is what i try because everything else seems to fail as a concept.
thx.
答案1
得分: 0
如果我理解正确,您在数据库中拥有存储在 /users
下的各个用户的数据。如果是这样的话,您可以使用以下代码显示每个用户的数据:
get(child(dbRef, 'users/')).then((snapshot) => {
snapshot.forEach((userSnapshot) => {
console.log(userSnapshot.key); // 显示用户节点的键
console.log(userSnapshot.val()); // 显示用户节点的整个值
console.log(userSnapshot.child("uid").val()); // 仅显示用户节点的属性
})
});
英文:
If I understand correctly, you have data for individual users under /users
in your database. If that is the case, you can show the data for each user with:
get(child(dbRef, 'users/')).then((snapshot) => {
snapshot.forEach((userSnapshot) => {
console.log(userSnapshot.key); // show the key of the user node
console.log(userSnapshot.val()); // show the entire value of the user node
console.log(userSnapshot.child("uid").val()); // show only property of the user node
})
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论