ошибка при получении информации из firestore через облачные функции - PullRequest
0 голосов
/ 08 мая 2020

это скриншот того, как выглядит моя база данных firestore.

enter image description here

Я пытаюсь получить эту информацию из index. js файл в облачные функции мой индекс. 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);

когда я запускаю команду: 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.
>  (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)

// ------------ ОБНОВЛЕНИЕ --------------------------- //

После использования:

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

вместо моего предыдущего домашнего маршрута я все еще получаю следующую ошибку, которую я не могу понять. ОШИБКА:

    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)

помогите пожалуйста. спасибо

1 Ответ

1 голос
/ 08 мая 2020

Как указано в документации , вам необходимо отправить ответ клиенту, чтобы завершить функцию. Если вы не отправите ответ, время ожидания функции истечет. Если вы пытаетесь отправить все документы обратно, вам нужно будет написать что-то вроде этого:

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