Firebase emulator return empty data whereas working fine after deploying

こ雲淡風輕ζ 提交于 2020-05-15 08:06:59

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/61427487/firebase-emulator-return-empty-data-whereas-working-fine-after-deploying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!