Firebase-tools: firebase deploy return an empty object {}

Created on 17 Jun 2020  路  13Comments  路  Source: firebase/firebase-tools

[REQUIRED] Environment info

"firebase-admin": "^8.10.0",

"firebase-functions": "^3.6.1"

firebase tools: 8.4.2
node version: v12.16.3

Windows 10 OS
VS code

Platform:

[REQUIRED] Test case

As i am an absolute newbie in programming and development, i ahve followed the firebase youtube channel to try to use firebase cloud functions. So on the second video i ahve created the function in TS. Convereted it into JS. Deployed it and it works fine i.e. i can see the data from doc. So the next step was to use "firebase serve" instead of deploy. I have followed everything, but whne i use curl with the localhost link or even jsut a pure link, the output is an empty object {}.

[REQUIRED] Steps to reproduce

This is my code:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';


admin.initializeApp()


 export const getTask = functions.https.onRequest((request, response) => {
   admin.firestore()
   .collection('Rooms')
   .doc('RRN')
   .collection('Alpha')
   .doc('Task1')
   .get()
   .then(snapshot => {
      const data = snapshot.data()
      response.send(data)
  })
  .catch(error => {
      //Handle erroor
      console.log(error);
      response.status(500).send(error)
  })
 });

[REQUIRED] Expected behavior

Expected to return the data from the doc.

[REQUIRED] Actual behavior

instead, i jsut get this:
{}

Any help guyz???

All 13 comments

Hi @dvijeniii05, welcome! I was able to run the code you provided locally and see data from Firestore, so I'll need a little bit more info to help with your issue:

  • What does your Firestore look like? What data is in the doc at Rooms/RRN/Alpha/Task1?
  • What data do you get back from the deployed function?
  • Could you try running locally again with the debug flag enabled ( firebase serve --debug), and copy paste the output here?

Hi Sir. Tanks for your reply.

This is the data located in the doc:

taskText: "First Task"

This is what i get when i use firebase deploy:

{"taskText":"First Task"}

So, i ahve run firebase serve --debug, and have received the following:

$ firebase serve --debug
[2020-06-17T10:16:08.893Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2020-06-17T10:16:08.895Z] > authorizing via signed-in user
[2020-06-17T10:16:08.895Z] [iam] checking project super-couples for permissions ["firebase.projects.get"]
[2020-06-17T10:16:08.899Z] >>> HTTP REQUEST POST https://cloudresourcemanager.googleapis.com/v1/projects/super-couples:testIamPermissions
 {"permissions":["firebase.projects.get"]}
[2020-06-17T10:16:09.537Z] <<< HTTP RESPONSE 200 {"content-type":"application/json; charset=UTF-8","vary":"X-Origin, Referer, Origin,Accept-Encoding","date":"Wed, 17 Jun 2020 10:16:08 GMT","server":"ESF","cache-control":"private","x-xss-protection":"0","x-frame-options":"SAMEORIGIN","x-content-type-options":"nosniff","server-timing":"gfet4t7; dur=553","alt-svc":"h3-28=\":443\"; ma=2592000,h3-27=\":443\"; ma=2592000,h3-25=\":443\"; ma=2592000,h3-T050=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-Q048=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"","accept-ranges":"none","transfer-encoding":"chunked"}
[2020-06-17T10:16:09.539Z] >>> HTTP REQUEST GET https://firebase.googleapis.com/v1beta1/projects/super-couples

[2020-06-17T10:16:09.746Z] <<< HTTP RESPONSE 200 {"content-type":"application/json; charset=UTF-8","vary":"X-Origin, Referer, Origin,Accept-Encoding","date":"Wed, 17 Jun 2020 10:16:08 GMT","server":"ESF","cache-control":"private","x-xss-protection":"0","x-frame-options":"SAMEORIGIN","x-content-type-options":"nosniff","alt-svc":"h3-28=\":443\"; ma=2592000,h3-27=\":443\"; ma=2592000,h3-25=\":443\"; ma=2592000,h3-T050=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-Q048=\":443\"; ma=2592000,h3-Q046=\":443\";
ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"","accept-ranges":"none","transfer-encoding":"chunked"}

=== Serving from 'D:\SuperCouples\cloudFunc'...

!  Your requested "node" version "8" doesn't match your global version "12" {"metadata":{"emulator":{"name":"functions"},"message":"Your requested \"node\" version \"8\" doesn't match your global version \"12\""}}
i  functions: Watching "D:\SuperCouples\cloudFunc\functions" for Cloud Functions... {"metadata":{"emulator":{"name":"functions"},"message":"Watching \"D:\\SuperCouples\\cloudFunc\\functions\" for Cloud Functions..."}}
[2020-06-17T10:16:10.774Z] [worker-pool] addWorker(~diagnostic~) {"metadata":{"emulator":{"name":"functions"},"message":"[worker-pool] addWorker(~diagnostic~)"}}
[2020-06-17T10:16:10.775Z] [worker-pool] Adding worker with key ~diagnostic~, total=1 {"metadata":{"emulator":{"name":"functions"},"message":"[worker-pool] Adding worker with key ~diagnostic~, total=1"}}
[2020-06-17T10:16:10.775Z] [worker-pool] submitWork(triggerId=) {"metadata":{"emulator":{"name":"functions"},"message":"[worker-pool] submitWork(triggerId=)"}}
[2020-06-17T10:16:10.777Z] [worker-~diagnostic~-a6e470c2-025c-4693-be39-09f155a4c3bf]: Assigning socketPath:
\\?\pipe\D:\SuperCouples\cloudFunc\functions\9716 {"metadata":{"emulator":{"name":"functions"},"message":"[worker-~diagnostic~-a6e470c2-025c-4693-be39-09f155a4c3bf]: Assigning socketPath: \\\\?\\pipe\\D:\\SuperCouples\\cloudFunc\\functions\\9716"}}
[2020-06-17T10:16:10.779Z] [worker-~diagnostic~-a6e470c2-025c-4693-be39-09f155a4c3bf]: BUSY {"metadata":{"emulator":{"name":"functions"},"message":"[worker-~diagnostic~-a6e470c2-025c-4693-be39-09f155a4c3bf]: BUSY"}}
[2020-06-17T10:16:11.119Z] [runtime-status] [9716] Functions runtime initialized. {"cwd":"D:\\SuperCouples\\cloudFunc\\functions","node_version":"12.16.3"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Functions runtime initialized. {\"cwd\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\",\"node_version\":\"12.16.3\"}"}}
[2020-06-17T10:16:11.123Z] [runtime-status] [9716] Disabled runtime features: undefined {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Disabled runtime features: undefined"}}
[2020-06-17T10:16:11.132Z] [runtime-status] [9716] Resolved module firebase-admin {"declared":true,"installed":true,"version":"8.12.1","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-admin\\lib\\index.js"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Resolved module firebase-admin {\"declared\":true,\"installed\":true,\"version\":\"8.12.1\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-admin\\\\lib\\\\index.js\"}"}}
[2020-06-17T10:16:11.139Z] [runtime-status] [9716] Resolved module firebase-functions {"declared":true,"installed":true,"version":"3.7.0","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-functions\\lib\\index.js"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Resolved module firebase-functions {\"declared\":true,\"installed\":true,\"version\":\"3.7.0\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-functions\\\\lib\\\\index.js\"}"}}
[2020-06-17T10:16:11.144Z] [runtime-status] [9716] Found local functions config: D:\SuperCouples\cloudFunc\functions/.runtimeconfig.json {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Found local functions config: D:\\SuperCouples\\cloudFunc\\functions/.runtimeconfig.json"}}
[2020-06-17T10:16:11.145Z] [runtime-status] [9716] Outgoing network have been stubbed. [{"name":"http","status":"mocked"},{"name":"http","status":"mocked"},{"name":"https","status":"mocked"},{"name":"https","status":"mocked"},{"name":"net","status":"mocked"}] {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Outgoing network have been stubbed. [{\"name\":\"http\",\"status\":\"mocked\"},{\"name\":\"http\",\"status\":\"mocked\"},{\"name\":\"https\",\"status\":\"mocked\"},{\"name\":\"https\",\"status\":\"mocked\"},{\"name\":\"net\",\"status\":\"mocked\"}]"}}
[2020-06-17T10:16:11.151Z] [runtime-status] [9716] Resolved module firebase-functions {"declared":true,"installed":true,"version":"3.7.0","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-functions\\lib\\index.js"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Resolved module firebase-functions {\"declared\":true,\"installed\":true,\"version\":\"3.7.0\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-functions\\\\lib\\\\index.js\"}"}}
[2020-06-17T10:16:11.560Z] [runtime-status] [9716] Checked functions.config() {"config":{"someservice":{"key":"THE API KEY","id":"THE CLIENT ID"}}} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Checked functions.config() {\"config\":{\"someservice\":{\"key\":\"THE API KEY\",\"id\":\"THE CLIENT ID\"}}}"}}
[2020-06-17T10:16:11.561Z] [runtime-status] [9716] firebase-functions has been stubbed. {"functionsResolution":{"declared":true,"installed":true,"version":"3.7.0","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-functions\\lib\\index.js"}} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] firebase-functions has been stubbed. {\"functionsResolution\":{\"declared\":true,\"installed\":true,\"version\":\"3.7.0\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-functions\\\\lib\\\\index.js\"}}"}}
[2020-06-17T10:16:11.561Z] [runtime-status] [9716] Resolved module firebase-functions {"declared":true,"installed":true,"version":"3.7.0","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-functions\\lib\\index.js"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Resolved module firebase-functions {\"declared\":true,\"installed\":true,\"version\":\"3.7.0\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-functions\\\\lib\\\\index.js\"}"}}
[2020-06-17T10:16:11.562Z] [runtime-status] [9716] Resolved module firebase-admin {"declared":true,"installed":true,"version":"8.12.1","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-admin\\lib\\index.js"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Resolved module firebase-admin {\"declared\":true,\"installed\":true,\"version\":\"8.12.1\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-admin\\\\lib\\\\index.js\"}"}}
[2020-06-17T10:16:11.563Z] [runtime-status] [9716] Resolved module firebase-functions {"declared":true,"installed":true,"version":"3.7.0","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-functions\\lib\\index.js"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] Resolved module firebase-functions {\"declared\":true,\"installed\":true,\"version\":\"3.7.0\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-functions\\\\lib\\\\index.js\"}"}}
[2020-06-17T10:16:11.564Z] [runtime-status] [9716] firebase-admin has been stubbed. {"adminResolution":{"declared":true,"installed":true,"version":"8.12.1","resolution":"D:\\SuperCouples\\cloudFunc\\functions\\node_modules\\firebase-admin\\lib\\index.js"}} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] firebase-admin has been stubbed. {\"adminResolution\":{\"declared\":true,\"installed\":true,\"version\":\"8.12.1\",\"resolution\":\"D:\\\\SuperCouples\\\\cloudFunc\\\\functions\\\\node_modules\\\\firebase-admin\\\\lib\\\\index.js\"}}"}}
[2020-06-17T10:16:11.570Z] [runtime-status] [9716] initializeApp(DEFAULT) {"databaseURL":"https://super-couples.firebaseio.com","storageBucket":"super-couples.appspot.com","projectId":"super-couples"} {"metadata":{"emulator":{"name":"functions"},"message":"[runtime-status] [9716] initializeApp(DEFAULT) {\"databaseURL\":\"https://super-couples.firebaseio.com\",\"storageBucket\":\"super-couples.appspot.com\",\"projectId\":\"super-couples\"}"}}
+  functions[getTask]: http function initialized (http://localhost:5000/super-couples/us-central1/getTask). {"metadata":{"emulator":{"name":"functions"},"message":"\u001b[1mhttp\u001b[22m function initialized (http://localhost:5000/super-couples/us-central1/getTask)."}}
[2020-06-17T10:16:11.578Z] [worker-~diagnostic~-a6e470c2-025c-4693-be39-09f155a4c3bf]: IDLE {"metadata":{"emulator":{"name":"functions"},"message":"[worker-~diagnostic~-a6e470c2-025c-4693-be39-09f155a4c3bf]: IDLE"}}

Sorry to keep asking for more info - could you try the following and post the logs you get?
1 - Replace your code with this snippet, which just has some debug logging added:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp()

export const getTask = functions.https.onRequest((request, response) => {
admin.firestore()
.collection('Rooms')
.doc('RRN')
.collection('Alpha')
.doc('Task1')
.get()
.then(snapshot => {
  console.log(snapshot) // This is the only change.
  const data = snapshot.data()
  response.send(data)
})
.catch(error => {
  //Handle error
  console.log(error);
  response.status(500).send(error)
})
});

2 - firebase deploy the new code and trigger getTask, then go to https://console.firebase.google.com/project/joe-tests-ext/functions/logs and copy paste the logs for that run here.

3- firebase serve the new code and trigger getTask, then copy paste the logs here.

HI there. Not a problem:

so, this is what i get when use firebase deploy in ly log:

QueryDocumentSnapshot {
  _fieldsProto: 
   { taskText: { stringValue: 'First Task', valueType: 'stringValue' } },
  _ref: 
   DocumentReference {
     _firestore: 
      Firestore {
        _settings: [Object],
        _settingsFrozen: true,
        _serializer: [Object],
        _projectId: 'super-couples',
        registeredListenersCount: 0,
        _lastSuccessfulRequest: 1592412440158,
        _backoffSettings: [Object],
        _preferTransactions: true,
        _clientPool: [Object] },
     _path: ResourcePath { segments: [Array] },
     _converter: 
      { toFirestore: [Function: toFirestore],
        fromFirestore: [Function: fromFirestore] } },
  _serializer: Serializer { createReference: [Function], allowUndefined: false },
  _readTime: Timestamp { _seconds: 1592412440, _nanoseconds: 81494000 },
  _createTime: Timestamp { _seconds: 1592147592, _nanoseconds: 138651000 },
  _updateTime: Timestamp { _seconds: 1592147592, _nanoseconds: 138651000 } } 

However when i use serve, i get no new logs in the console at all. In the terminal i get this:
firebase serve

=== Serving from 'D:\SuperCouples\cloudFunc'...

!  Your requested "node" version "8" doesn't match your global version "12"
i  functions: Watching "D:\SuperCouples\cloudFunc\functions" for Cloud Functions...
+  functions[getTask]: http function initialized (http://localhost:5000/super-couples/us-central1/getTask).
i  functions: Beginning execution of "getTask"
!  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
!  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.
>  Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (D:\SuperCouples\cloudFunc\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 (D:\SuperCouples\cloudFunc\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:502:17)
>      at async GrpcClient._getCredentials (D:\SuperCouples\cloudFunc\functions\node_modules\google-gax\build\src\grpc.js:92:24)
>      at async GrpcClient.createStub (D:\SuperCouples\cloudFunc\functions\node_modules\google-gax\build\src\grpc.js:213:23)
>  Caused by: Error
>      at Firestore.getAll (D:\SuperCouples\cloudFunc\functions\node_modules\@google-cloud\firestore\build\src\index.js:683:23)
>      at DocumentReference.get (D:\SuperCouples\cloudFunc\functions\node_modules\@google-cloud\firestore\build\src\reference.js:199:32)
>      at D:\SuperCouples\cloudFunc\functions\lib\index.js:13:10
>      at C:\Users\veryrare\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:559:20
>      at C:\Users\veryrare\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:534:19
>      at Generator.next (<anonymous>)
>      at C:\Users\veryrare\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:8:71
>      at new Promise (<anonymous>)
>      at __awaiter (C:\Users\veryrare\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:4:12)
>      at runFunction (C:\Users\veryrare\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:531:12)
i  functions: Finished "getTask" in ~8s

So, it looks like you are running into some issues with your Application Default Credentials. This tends to be a tricky subject, but I think this Stack Overflow poster had the same issue as you.

Could you try running firebase login --reauth, then try firebase serve again?

I have done the reauth bit, but it ends up the same. Trying to follow the solutions you have jsut linked. There is a lot and all different. But it seems like i defo dont want to put the project credentials into the code. Tried await/async - does not change anything.

Ah ok I know what's going on here. This is a version of https://github.com/firebase/firebase-tools/issues/1940

Basically right now firebase login actually doesn't have anything to do with the authentication you'll need for code running inside the functions emulator.

If you want your Functions emulator to communicate with Cloud Firestore in production you will need to authenticate your local environment with the gcloud tool:

gcloud auth application-default login

This is something I am actively working on improving in #2214

Also @dvijeniii05 just a general tip: on Github you can use triple backticks for code blocks:

````

Your code

````

So then you get nice formatting like this:

function myFunction() {
  console.log("Hello, world!");
}

Thanks a lot for your replies @samtstern . I will follow your advice to improve the code formatting.

Just would like to confirm, as per your advice, i should just run this in my terminal?

```

gcloud auth application-default login

```

Also, does this mean that i will have to create a new project in google cloud, or can i access my firebase project??

@dvijeniii05 you will need to install the gcloud CLI and then yes, just run that command at your terminal:
https://cloud.google.com/sdk/docs/quickstarts

You won't need a new project and it won't even ask about it. That's just the gcloud version of firebase login. It will add a credential to your machine which firebase-admin will be able to discover.

@samtstern i have installed the CLI.
When i try to run the provided command i receive the following in my terminal:

/d/cloudCLI/google-cloud-sdk/bin/gcloud: line 195: exec: python: not found

Hi @samtstern . I have fixed the issue with python not found.

After i have run your suggested command, i run firebase serve, then curl given-link
and now i receive the right object back

$ curl http://localhost:5000/super-couples/us-central1/maybeTask
{"taskText":"First Task"}

Thanks a lot for your help guyz @samtstern @joehan
Appreciate it a lot.

Very glad it worked, thanks for your patience!

Was this page helpful?
0 / 5 - 0 ratings