How to write a new document of a Firebase Collection taking the given userId key(UID)


#1

Hi Antti. Any tip in how to achieve writing a new document inside a FB Collection taking the given userId key(UID). I’m trying to do this giving the path of the collection as you can see in the attached image. Considering that I am already getting the UID using the the data Slot UserID this would be possible, right? However nothing happens, when I save a new document, It behaves like there’s no /Users/$slot(‘ds_SlotuserID’) declare. Thank you. Best, Marcos


#2

Ok. I see now. The UserID was lowercase. Now I got this


#3

Guys. Still lost and walking in circles. What I am looking for is writing the document key with the same key given by UID of Firebase, to be able to associate it to Users collection where you already find a column with the same userID keys for each user. Data recorded when users finalize their registration (after authenticating with google) given them more information such as Address, Places, etc

Possible using React Studio Right? I suspect it is. If done, I can say that my project is not far from going to deploy. Thank you again


#4

Hi,

I’m not 100% sure but I think this happens because Studio does not know if you’re adding or editing existing or adding a new document. Unfortunately when you want to edit document you need to go to Edit screen through a list.

If you’re trying to create document with certain document name then unfortunately I think it is not possible at the moment. Currently document key is always automatically generated UUID.


#5

Hi Annti. Thanks for your time. See, I’m already saving and editing a document just fine. Here’s how it works:
When you sign in using Google, automatically you have the userID, userName, userPhoto and userEmail. Great, For my app, I just need to confirm the last information which is the user current home address, including city and neighborhood. From a list of communities, first, you choose or search for your neighborhood and then you will be taken to another screen where you provide your addresses. Clicking “Finish” creates the user in the “Users” collection containing the necessary data that allows associating the user with the neighborhood key. Both collections bring the same ID. Thank you for your help. Best, Marcos

!


#6

Ok then this looks quite simple and saving user information is already working on your end?

I think you need to somehow pull the neighborhoodID into Data slot automatically when user logins to app. Maybe this could be done via Screen Script? I mean so that you have one Data sheet which queries users collection based on the user id and contains one row for logged in user. Then simply save the neighbour ID to data slot via script (How to access datasheets from javascript functions).


#7

Yes, that’s right. Well, sounds great Antti. Let try this.


#8

Hi Antti, I tried in many ways, but it didn’t work. I checked the link, but it was not clear to me how to do this. Any clues on how to make this script simple? Thanks a lot. Best


#9

I tested this a bit and it’s not that simple to save the value from Data sheet to Data slot. This would need to be added into Screen state script and is so hacky that I don’t want to share it :wink:

I’d suggest you to edit the Firebase Login plugin so that it would load the users data from Firestore and then would save it to Data slot. I’d probably add this code into “saveCurrentUserDataInApp” function which get’s executed after the successful login.

P.S. This is my hacky solution which I added to screen’s State script. Note that you need to have at least two states and data slot selected from State setting to get the script written into exported code.

var user=this.props.appActions.getDataSheet(‘users’).items[0]; //Get users data sheet
//If user is not undefined
if (user){
var userFavCity=user[‘favoriteCityId’]; //get the favoriteCityId columns value into variable
this.props.appActions.dataSlots[‘ds_SlotFavoriteCityID’]=userFavCity; // save the value to data slot
}


#10

Hey Antti. Thank you. Glad you share this. I’ve worked according to your hacky solution and the plugin as well but apparently something still missing. Best, Marcos


#11

Hi,

In the script you’re setting the data slot value to string ‘userNeighbourHoodsID’. I’m guessing you should have userNeighbourHoodsID variable there instead of the string. Just remove the ‘’ from the code. Anyway you need to debug the code from sources tab from developer tools to find out what is causing the problems.