Function CollectionRefernce.doc() requires its first argument to be of type string

#1

Hey All

I’m working on React-native and Firestore. To add the data in the firestore the following is the code

componentWillMount() {

      AsyncStorage.getItem("logid").then((value) => {
            this.setState({"logid":JSON.parse(value) });
        }).done();

        firebase.firestore().collection('mainmhealthdata').doc(this.state.logid).set({
          insta: 0
        })

I’m storing the logid in AsyncStorage and getting it into another screen in componentWillMount(). The above code for firestore is working fine on click events only but for react-native life cycle components its not working and the following error occurs:

Can anyone please help me out?

Thank you in advance

0 Likes

#2

Hey @kaynanguarany,

this.state.logid is undefined because your firebase function is running before your logid is fetched from AsyncStorage. You need to use an async function and use await so that the firebase function doesn’t execute until after AsyncStorage fetches the value.

Cheers,

Adam

0 Likes

#3

thnq @adamjnav

it worked…

0 Likes

closed #4

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.

0 Likes