简体   繁体   English

从node.js中的firebase实时数据库获取数据时如何通过函数返回数据

[英]how to return data through function when fetching data from firebase realtime-database in node.js

How can I get this key in DATA and use this key outside of the function?如何在 DATA 中获取此键并在函数之外使用此键?

let DATA = [];

  const database = admin.database();
  let keyref = database.ref("data");

  keyref.once("value", async function (snapshot) {
    let key = await snapshot.val(); // i want this key data out side this function
    DATA.push(key);
    console.log(DATA);
  });

 console.log(DATA); // i want here that inside function key

In short, I want fetched data outside the function简而言之,我想在函数之外获取数据

async function getData() {
  console.log("Refreshing Firebase Credentials");
  const keyref = database.ref("data");
  const snapshot = await keyref.get();

  if (snapshot.exists()) {
    const snapshotVal = snapshot.val();
    console.log(snapshotVal);
    credentials = {
      expires_at: new Date(snapshotVal.expires_at),
      enc_key: snapshotVal.enc_key,
      iv: snapshotVal.iv,
    };
  } else {
    console.log("Firebase Credentials not found");
    process.exit(1);
  }
}

module.exports = {
  getData
};

Use this function where you want to this data在您想要此数据的地方使用此功能

When fetching RTDB data only once, it is advised to use the get() method.当只获取一次 RTDB 数据时,建议使用get()方法。 This method is asynchronous, so you need to do something along the following lines:此方法是异步的,因此您需要按照以下几行做一些事情:

  async function getRTDBData(ref) {
    const database = admin.database();
    const keyref = database.ref("data");
    const snapshot = await keyref.get();

    if (snapshot.exists()) {
        return snapshot.val();
    } else {
       return .... // Up to you to adapt here
    }
  }

  getRTDBData("data")
  .then(val => {
      // Do what you want with val
      cosniole.log(val);
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 firebase 实时数据库中获取数据时如何通过 function 返回数据 - how to return data through function when fetching data from firebase realtime-database in react native node.js和hapi:从数据库同步获取数据 - node.js and hapi: fetching data from a database synchronously 如何使用firebase-admin模块使用Node.js从Firebase数据库返回有序数据? - How can I return ordered data from Firebase database with Node.js using the firebase-admin module? 如何在没有Node.js的情况下使用Firebase实时数据库 - How to use firebase realtime database without node.js 如何在 React js 中从 firebase 实时数据库中获取数据 - how to fetch data from firebase realtime database in react js 从 firebase 实时数据库获取数据返回无效的 json 响应 - Fetching data from firebase realtime database returns invalid json response 如何使用 Vue.js 从 Firebase 实时数据库读取数据 - How to read data from Firebase Realtime Database with Vue.js 如何在 firebase 实时数据库规则中设置变量 - How to set a variable in firebase realtime-database rules Node.js-该函数不返回表中选择的数据 - Node.js - The function does not return data in select from table 如何从Firebase实时数据库中检索数据? - How to retrieve data from firebase realtime database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM