[英]How do I return the snapshot value of my Firebase Realtime Database from a function?
I want to return the snapshot.val()
, of my Realtime Database:我想返回实时数据库的snapshot.val()
:
function get_details(){
get(ref(db,"pay/0/0/")).then((snapshot) => {
if (snapshot.exists()) {
var resp = [];
resp.push(snapshot.val());
return resp;
}
}
}
let response = get_details();
console.log(response); //undefined
I keep getting undefined
as the returned value.我一直将undefined
作为返回值。 I have followed the answer from this post.我已经关注了这篇文章的答案。
How do I resolve this issue?我该如何解决这个问题?
If you are calling get_details
from somewhere else in the code.如果您从代码中的其他地方调用get_details
。 Try:尝试:
async function get_details() {
const snapshot = await get(ref(db, "pay/0/0/"))
return snapshot.val()
}
// get_details() also returns a Promise.
get_details().then((response) => {
// continue
console.log('Value is', response)
})
// Alternatively, if parent function is async
const mainFunc = async () => {
const response = await get_details()
}
Also checkout How do I return the response from an asynchronous call?另请查看如何从异步调用返回响应?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.