遍历 Firebase 数据库而无需使用 Google Firebase 云函数?

huangapple go评论55阅读模式
英文:

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
})
});

huangapple
  • 本文由 发表于 2023年5月29日 16:44:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定