3

I have a written a very basic API which will return the services. I tried to run this API in emulator but it return the empty data

{
    "status": "success",
    "statusCode": 200,
    "message": "Services retrieved",
    "data": []
}

I have setup the firestore, functions and database emulators. And I am using

"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.0"

Any idea why the data response is empty ?

EDIT

This is my method to call the service

export const activeServices = functions.https.onRequest((request, response) => {
    let services = new Array<string>();
    admin.firestore().collection(newServices).get()
    .then(serviceSnapshot => {
        serviceSnapshot.docs.forEach(doc => {
            if(doc.data().service_active){
                services.push(doc.data().service_name)
            }
        })
        const successResponse = common.success("success", 200, "Services retrieved", services)
        response.send(successResponse)
    })
    .catch(error => {
        const errorResponse = common.error("fail", 500, "Failed to get active services")
        console.error(errorResponse)
        response.send(errorResponse)
    })
})

I tried to execute this and it return nothing and executed the same functions after deploying. And I got the response. From below answers I think running only functions will try to communicate with production database.

firebase emulators:start --only functions

functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
⚠  External network resource requested!
   - URL: "http://xxx.xxx.xxx.xxx/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.

This looks like it's trying to communicate to production but couldn't make any successful request.

3
  • 1
    Feel free to share some code so we can better see the problem.. Commented Apr 25, 2020 at 14:50
  • I don't think it has to do something with code. I think it's more to do with some wrong configurations. Because I tried to deploy the function and it's working. But not with emulators Commented Apr 25, 2020 at 19:18
  • @Ryan updated the question Commented Apr 26, 2020 at 3:53

2 Answers 2

2

If it's not working in the emulator, but working in deployment, it suggests that your emulated firestore does not have the data.

Try this: run Firebase:emulators start --only functions so that the database in use is your production database. (Obviously do so with caution if you're going to manipulate the data there.)

Then run the emulated function against your production database. If you get the data you want, probably the issue is that your emulated firestore.

Personally, I have found that working with emulated functions but non emulated fire store is the best testing flow for me, where I create a duplicate section of my production database for testing purposes. This still allows niceties like hot reloading, but I find the behavior to mimic production more closely and predictably. Then, you can have the functions intelligently point toward the appropriate section of the database depending on whether you are emulating or not.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. It makes sense. You mentioned "making duplicate section of production db for testing", how would I achieve this ? How can I make a duplicate section of production db and that db is accessible from local ?
The local emulator accesses production Firestore if you use --only functions, because there is no emulated Firestore in that case. To duplicate your data, you could have top-level collections like production and development, and use a cloud function to sync them however you wish. Or do it once, manually, using imports/exports: firebase.google.com/docs/firestore/manage-data/export-import
I tried earlier the same way by running function emulator. But didn't get the data. I got this error "functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production. ⚠ External network resource requested!"
Yes, that is the CLI letting you know that you are connecting to your production database. It doesn't have to mean that you are affecting your production data, as long as you have a separate collection for test data. It's really up to you how you structure your development flow, I was just describing what works for me.
If that's connected to the production database, then why I am not able to retrieve the data ?
|
0

I was stuck on this problem for a couple of hours. Things worked fine with the deployed firestore but emulated firestore always gave back empty responses.

Turned out, I had incorrectly seeded data in the emulated firestore. After I fixed that, things were fine so make sure your data is fine in the emulator.

I just don't like the idea of using production firestore for testing/debugging. Yes, you could "create a duplicate section in your production db for testing" but duplicating all that data comes at a cost. You're better off saving your $s and using firestore emulator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.