cache things
cache things

Reputation: 1

How to add data in firebase with React Native app?

code:
  const timeStamp = Math.floor(Date.now() / 1000);
        const insertKey =   "_" + timeStamp;
        const contactsDbRef = firebase.app().database().ref('contacts/');
        console.log(timeStamp+"/"+insertKey);
        contactsDbRef
             .child(insertKey)
            .set({
                
                name:name,
                number: number,
                email: email,
                
            },
            console.log("data added"),
                (error) => {
                    if (error) {
                        console.log(error)
                    } else {
                        
                        console.log("added successfully")
                       
                    }
                });
       
       

How do i store contact details of user in firebase in react native app?

It is console logging the data, however in the backend data is not reflecting

Upvotes: 0

Views: 1205

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

I would suggest to try Firebase operations using promises and not error callbacks as in the docs.

const timeStamp = Math.floor(Date.now() / 1000);
const insertKey =   "_" + timeStamp;
        
const contactsDbRef = firebase.app().database().ref('contacts/'+insertKey);

contactsDbRef.set({
  name:name,
  number: number,
  email: email,
}).then(() => {
  console.log("Data updated")
}).catch(e => console.log(e))

I tried this out and worked perfectly. Let me know if the issue still persists.

Upvotes: 1

Related Questions