0

databaseReference:

example of problem

Im trying to save data to my firebase realtime database and I need my key to be the users ID and not the random generated unique key I tried child but still got some random keys any help? this is the method I currently have

async componentWillMount() {


const {data} = await Contacts.getContactsAsync({
  fields: [Contacts.Fields.PhoneNumbers],
});

this.setState({dataSource: data.map(contact => ({...contact, key: contact.number}))});

const contacts = data.filter(d => d.phoneNumbers);

contactsToUpload.forEach(contact => {
  const number = contact.phoneNumbers[0].number.replace(/\D/g, '');
  const ref = firebase.database().ref('/Contacts/' + number);
  try {
    ref.push(contact.name);
  } catch (error) {
    console.log(error);
  }
});
}
//Currently have
"Contacts" : {
  "5554787672" : {
    "-LWIlxwIETK5UR3O5GkR" : "Daniel Higgins Jr.",
    "-LWImsOurEVDOE-KrkVw" : "Daniel From School"
 }
//Needed
"Contacts" : {
 "5554787672" : {
  "UserId1" : "Daniel Higgins Jr.",
  "UserId2" : "Daniel From School"
 }
3
  • How is contactsToUpload populated/initialized? Note that it's easiest to help if the MCVE you share is standalone, so has no external dependencies beyond what you shared. Commented Jan 16, 2019 at 5:37
  • contactsToUpload is in componentWillMount on the HomeScreen and the variable contact is just the phone numbers from the device I have added the dependencies
    – J.lan
    Commented Jan 16, 2019 at 16:02
  • I think it's just const ref = firebase.database().ref('/Contacts/' + number); try { ref.set(contact.name); now. Commented Jan 17, 2019 at 2:16

2 Answers 2

1
db.ref('UsersList/').child('name you want to use').set({
    email:this.state.email,
    fname: this.state.lastname,
    lname: this.state.firstname,
}).then((data)=>{
    //success callback
    alert('success'+data);
}).catch((error)=>{
    //error callback
    alert('failed'+error);
  })

something like this worked for me and I was able to store based on special usernames I needed.

1
  • the .child in the first line will help set a child key under the UserList Commented Mar 22, 2019 at 17:36
0

Every time you call push() Firebase generates a reference to a new, unique location in the database. So ref.push() creates a new, unique location under ref.

If you want to simply set the name under the contact's ID, change ref.push(contact.name); to:

ref.set(contact.name);
6
  • Hi. Thanks for your response. Multiple users might upload the same number under different names I would like to keep as a key the user ID and as the value what that user names him I think set will delete the previous key as in I wont be able to keep the different names user have given to that number
    – J.lan
    Commented Jan 15, 2019 at 17:35
  • I don't understand what you're trying to accomplish. Can you show two calls that you'd like to run? And then also add the JSON (as text) that you want after those two calls? Commented Jan 15, 2019 at 21:56
  • every new user will have the app run the method given above on the first use to add their contacts to the database, After two user that both have the same number on the phone the database will have "Contacts" : { "5554787672" : { "-LWIlxwIETK5UR3O5GkR" : "Daniel Higgins Jr.", "-LWImsOurEVDOE-KrkVw" : "Daniel From School" } My ideal scenario is that I can attribute what user uploaded what by having the following "Contacts" : { "5554787672" : { "UserID1" : "Daniel Higgins Jr.", "UserID2" : "Daniel From School" } Thanks again for the help
    – J.lan
    Commented Jan 15, 2019 at 23:15
  • Have : "Contacts" : { "5554787672" : { "-LWIlxwIETK5UR3O5GkR" : "Daniel Higgins Jr.", "-LWImsOurEVDOE-KrkVw" : "Daniel From School" } Need: "Contacts" : { "5554787672" : { "UserID1" : "Daniel Higgins Jr.", "UserID2" : "Daniel From School" }
    – J.lan
    Commented Jan 15, 2019 at 23:17
  • Please add the information to your question and use the formatting tools of Stack Overflow to make it readable. Code and data in comments are notoriously hard to read. Commented Jan 16, 2019 at 0:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.