我想做一个简单的网站与Firebase功能。
- 我必须显示一个提示符来询问他们的导入id。
- 我将把答案设置为一个变量。然后用导入代码 导入他们的数据
以下是index.js
async function getFirestore() {
var importcode = prompt("What is the Import Code?");
const firestore_con = await admin.firestore();
const writeResult = firestore_con
.collection("Exports")
.doc(importcode)
.get()
.then((doc) => {
if (!doc.exists) {
console.log("No such document!");
} else {
return doc.data();
}
})
.catch((err) => {
console.log("Error getting document", err);
});
return writeResult;
}
和我得到这个在调试日志
i functions: Beginning execution of "us-central1-app"
> /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:19
> var importcode = prompt("What is the Import Code?");
> ^
>
> ReferenceError: prompt is not defined
> at getFirestore (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:19:22)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:39:27
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/route.js:137:13)
> at Route.dispatch (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/route.js:112:3)
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:281:22
> at Function.process_params (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:335:12)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:275:10)
> at expressInit (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/middleware/init.js:40:5)
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at trim_prefix (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:317:13)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:284:7
> at Function.process_params (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:335:12)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:275:10)
> at query (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/middleware/query.js:45:5)
我认为这是因为index.js
运行在服务器端,,但有任何替代方案来实现这一点?
PS:我对firebase只有非常基本的了解,所以这可能是一个愚蠢的问题,但仍然请帮助:)
prompt()
方法可以在浏览器中使用,而不是在服务器端,即在云功能中使用。我不确定你的前端看起来像什么,但你应该提示用户在你的web应用程序中输入ID,然后在云函数中传递它。使用可调用函数的简单示例:
var importcode = prompt("What is the Import Code?");
var getData = firebase.functions().httpsCallable('getData');
getData({ code: importCode })
.then((result) => {
// Read result of the Cloud Function.
var result = result.data;
console.log(result)
});
然后你可以在你的Cloud函数中像这样阅读这段代码:
exports.getData = functions.https.onCall((data, context) => {
console.log(data)
// data contains the import code
// run your Firestore query here
});