【问题标题】:How to get value from Firestore using Firebase Functions?如何使用 Firebase 函数从 Firestore 获取价值?
【发布时间】:2019-10-17 09:55:29
【问题描述】:

我想做一个非常基本的请求,以获取我的文档“LA”的文本值。 我需要在用它做其他事情之前检索该值。

async function getValue() {

  doc = await admin.firestore().doc('cities/LA').get();
  console.log('Authorized User Data From Function:', doc.data());
  result = doc.data().text;
  return result;
}

app.get('/hello-world',  async(req, res) => {

  console.log("before" );

  var text = getValue();

  console.log(text);
  //...do something with text

  console.log("after" );

  return res.status(200).send("sent !");

});
module.exports.app = functions.https.onRequest(app);

我部署时没有错误。

/Users/user/Documents/APP TEST/functions/index.js
  170:12  warning  Avoid nesting promises          promise/no-nesting
  170:12  warning  Avoid nesting promises          promise/no-nesting
  173:12  warning  Unexpected function expression  prefer-arrow-callback
  180:12  warning  Unexpected function expression  prefer-arrow-callback

✖ 4 problems (0 errors, 4 warnings)
  0 errors and 2 warnings potentially fixable with the `--fix` option.

✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (42.45 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating Node.js 8 function app(us-central1)...
i  functions: updating Node.js 8 function sendIAPAnalytics(us-central1)...
✔  scheduler: all necessary APIs are enabled
✔  functions[sendIAPAnalytics(us-central1)]: Successful update operation. 
✔  functions[app(us-central1)]: Successful update operation. 

✔  Deploy complete!

预期日志:

before
Authorized User Data From Function:
<my text value>
after

显示的日志:

>Function execution started
>Function execution took 517 ms, finished with status code: 200

没有别的了:(

怎么了?

我也试过这个帖子的解决方法,没有成功,什么都没有显示:https://stackoverflow.com/a/55240125/2123039

谢谢

编辑 1: 没有更多日志添加 try/catch 块:

async function getValue() {

  try {
    doc = await admin.firestore().collection('cities').doc("LA").get();
    console.log('Authorized User Data From Function:', doc.data());
    result = doc.data();
    return result;

  } catch(e) {
    console.log(e);
  }

}

【问题讨论】:

  • 你能不能把你的await admin.firestore().doc('cities/LA').get();写在一个try catch块中,检查是否有任何错误出现
  • @KevinRED,请看我的编辑,日志是一样的,所以空:(
  • 现在做一件事让你的控制台日志为 console.error。执行代码后,检查 firebase 控制台并过滤有错误的日志。大多数情况下,firebase 需要一些时间才能在云中显示日志。
  • 在调用异步函数getValue()时添加await,即var text = await getValue()

标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

通过async function getValue() {...},您声明了一个异步函数(这是正确的,因为get() 函数是异步的)。但是你需要按如下方式调用它

app.get('/hello-world',  async(req, res) => {

  console.log("before" );

  const text = await getValue();   // <- Here use await

  console.log(text);
  //...do something with text

  console.log("after" );

  return res.status(200).send("sent !");

});

【讨论】:

    猜你喜欢
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多