firebase-tools-ui
firebase-tools-ui copied to clipboard
References are not working as expected with Cloud Functions in the Emulator UI
I have two collections, users and services. Every service has a reference (reference type) to the user that created it. I wrote a cloud function to access the user object and set "isServiceProvider" to true every time a service is added to the services collection.
exports.updateProviderStatus = functions.firestore .document("services/{serviceId}") .onCreate((snap, context) => { const userRef = snap.data().user.ref; return userRef.update({isServiceProvier: true}); });
I tried "testing" the function through the Emulator UI where I would manually add a service with a reference to an existing user, but every time, the reference would change to an empty string, and the value in users collection wouldn't change.
I ended up just deploying since I was just experimenting this project anyways, and the function works. I used the same method of adding a service manually through the Firestore UI on the console.
When the reference changes to an empty string, is there any error in your browser developer tool? Also, would you mind uploading a screencast? These will help us pin down the issue.
I recorded screencast of the function failing on the emulator and succeeding online. https://youtu.be/Gt8R2pjPFOY
Here is the error from my debug file.
Jan 31, 2022 5:35:38 PM com.google.cloud.datastore.emulator.impl.events.FunctionsEmulatorEventPublisher sendHttpRequest WARNING: Unsuccessful HTTP response when publishing event for projects/fireship-firebase-basics-d77c5/triggers/us-central1-updateProviderStatus-0: 500 Internal Server Error TypeError: userRef.update is not a function at /home/ubuntu-hasar/github/firebasics/functions/index.js:14:22 at cloudFunction (/home/ubuntu-hasar/github/firebasics/functions/node_modules/firebase-functions/lib/cloud-functions.js:135:23) at /home/ubuntu-hasar/.nvm/versions/node/v17.4.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:560:16 at runFunction (/home/ubuntu-hasar/.nvm/versions/node/v17.4.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:547:15) at runBackground (/home/ubuntu-hasar/.nvm/versions/node/v17.4.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:559:11) at processBackground (/home/ubuntu-hasar/.nvm/versions/node/v17.4.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:542:11) at invokeTrigger (/home/ubuntu-hasar/.nvm/versions/node/v17.4.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:624:19) at handleMessage (/home/ubuntu-hasar/.nvm/versions/node/v17.4.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:717:15) at processTicksAndRejections (node:internal/process/task_queues:96:5) Jan 31, 2022 5:35:39 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection.
I have the similar problem with the Firebase Emulator UI - not saving the DocumentReference to the Firestore Emulator. And it doesn't even have to be embedded in the Map.
firebase --version 10.2.1
In the browser DevTools Network's tab I can see a POST request with a payload containing: "uid":{"stringValue":""}
Even when I set the type "reference" for the "uid" field and provided a correct path to an existing document.
Therefore I assume the problem is somewhere within the Emulator UI's React application.
Problem might be here: https://github.com/firebase/firebase-tools-ui/blob/master/src/components/Firestore/DocumentEditor/utils.ts#L157
Seems to be a duplicate of #554
Closing this as a duplicate of #554. Please refer to the other issue for updates.