gpt4 book ai didi

javascript - 如何使用 Firebase 函数从 Firestore 获取值(value)?

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:37 25 4
gpt4 key购买 nike

我想做一个非常基本的请求,以获取我的文档“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 block 的日志:

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

}

最佳答案

通过执行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 !");

});

关于javascript - 如何使用 Firebase 函数从 Firestore 获取值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58429835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com