简体   繁体   English

通过云功能从 Firestore 检索信息时出错

[英]error while retrieving information from firestore through cloud functions

this is the screenshot of how my firestore database looks.这是我的 Firestore 数据库外观的屏幕截图。

在此处输入图像描述

i am trying to retrieve this information from index.js file in cloud functions my index.js file looks like:我正在尝试从云函数中的 index.js 文件中检索此信息,我的 index.js 文件如下所示:

const functions = require('firebase-functions');
const express = require('express');
const firebase = require('firebase-admin');

const firebaseApp = firebase.initializeApp(
    functions.config().firebase
);
const db = firebaseApp.firestore();
const app = express();

app.set('view engine','ejs');
//-------------------------------------------------------------------//

app.get("/home",function(req,res){
    db.collection('users').get().then((snapshot)=>{
        console.log(snapshot.docs);
    });
});

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

whenn i run the command: firebase serve --only functions,hosting当我运行命令时: firebase 服务 --only 功能,托管
i am getting this error:我收到此错误:

⚠  Your requested "node" version "8" doesn't match your global version "12"
✔  functions: functions emulator started at http://localhost:5001
i  functions: Watching "/home/chahat/Desktop/firebase 2.0/functions" for Cloud Functions...
i  hosting: Serving hosting files from: public
✔  hosting: Local server: http://localhost:5000
⚠  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
✔  functions[app]: http function initialized (http://localhost:5001/samsungmap-17515/us-central1/app).
⚠  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i  functions: Beginning execution of "app"
i  functions: Finished "app" in ~1s
i  functions: Beginning execution of "app"
⚠  External network resource requested!
   - URL: "http://169.254.169.254/computeMetadata/v1/instance"
 - Be careful, this may be a production service.
⚠  External network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
 - Be careful, this may be a production service.
>  (node:6741) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
>      at processTicksAndRejections (internal/process/task_queues.js:97:5)
>      at async GoogleAuth.getClient (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
>      at async GrpcClient._getCredentials (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:92:24)
>      at async GrpcClient.createStub (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:213:23)
>  (node:6741) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
>  (node:6741) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
⚠  functions: Your function timed out after ~60s. To configure this timeout, see
      https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
>  /usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619
>                  throw new Error("Function timed out.");
>                  ^
>  
>  Error: Function timed out.
>      at Timeout._onTimeout (/usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619:23)
>      at listOnTimeout (internal/timers.js:549:17)
>      at processTimers (internal/timers.js:492:7)

//------------UPDATE---------------------------// // - - - - - - 更新 - - - - - - - - - - - - - -//

After using:使用后:

app.get("/home",function(req,res){
    db.collection('users').get().then((snapshot)=>{
        res.json(snapshot.docs)
    });
});

rather than my prev home route, i am still getting the following error which i am unable to understand.而不是我以前的回家路线,我仍然收到以下我无法理解的错误。 ERROR:错误:

    chahat@chahat:~/Desktop/firebase 2.0$ firebase serve --only functions,hosting
⚠  Your requested "node" version "8" doesn't match your global version "12"
✔  functions: functions emulator started at http://localhost:5001
i  functions: Watching "/home/chahat/Desktop/firebase 2.0/functions" for Cloud Functions...
i  hosting: Serving hosting files from: public
✔  hosting: Local server: http://localhost:5000
⚠  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
✔  functions[app]: http function initialized (http://localhost:5001/samsungmap-17515/us-central1/app).
⚠  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i  functions: Beginning execution of "app"
i  functions: Finished "app" in ~1s
i  functions: Beginning execution of "app"
⚠  External network resource requested!
   - URL: "http://169.254.169.254/computeMetadata/v1/instance"
 - Be careful, this may be a production service.
⚠  External network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
 - Be careful, this may be a production service.
⚠  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i  functions: Beginning execution of "app"
⚠  External network resource requested!
   - URL: "http://169.254.169.254/computeMetadata/v1/instance"
 - Be careful, this may be a production service.
⚠  External network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
 - Be careful, this may be a production service.
>  (node:16552) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
>      at processTicksAndRejections (internal/process/task_queues.js:97:5)
>      at async GoogleAuth.getClient (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
>      at async GrpcClient._getCredentials (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:92:24)
>      at async GrpcClient.createStub (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:213:23)
>  (node:16552) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
>  (node:16552) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
>  (node:16583) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
>      at processTicksAndRejections (internal/process/task_queues.js:97:5)
>      at async GoogleAuth.getClient (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
>      at async GrpcClient._getCredentials (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:92:24)
>      at async GrpcClient.createStub (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:213:23)
>  (node:16583) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
>  (node:16583) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
⚠  functions: Your function timed out after ~60s. To configure this timeout, see
      https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
>  /usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619
>                  throw new Error("Function timed out.");
>                  ^
>  
>  Error: Function timed out.
>      at Timeout._onTimeout (/usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619:23)
>      at listOnTimeout (internal/timers.js:549:17)
>      at processTimers (internal/timers.js:492:7)

please help.请帮忙。 thankyou谢谢你

As stated in the documentation , you need to send a response to the client in order to terminate the function.文档中所述,您需要向客户端发送响应以终止 function。 If you don't send a response, then the function will time out.如果您不发送响应,则 function 将超时。 If you are trying to send all the documents back, you will need to write something like this:如果您尝试将所有文件发回,则需要编写如下内容:

app.get("/home",function(req,res){
    db.collection('users').get().then((snapshot)=>{
        res.json(snapshot.docs)
    });
});

from my experience, timed out will raise if you do not send the status of the request, whether it's a success or failure.根据我的经验,如果您不发送请求的状态,无论是成功还是失败,都会引发超时。 I'm modified/added your code with return status of your request as success (status: 200)我修改/添加了您的代码,您的请求返回状态为成功(状态:200)

app.get("/home",function(req,res){
    db.collection('users').get().then((snapshot)=>{
        res.json(snapshot.docs)
        return res.status(200).send(snapshot.docs) // add return status
    });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM