Is it possible for me to record information using one user.uid in firebase?

Asked

Viewed 69 times

0

I have no knowledge about the area, so at the moment I have many doubts and one of them is this:

I have a script that the proposal is to get the user.uid when it is logged in via email and password from the auth firebase and by using.uid to save the change in firebase, the structure from where it should be saved and the following users --> user.uid que no banco fica assim: cB9r9Y4zQcZ81cKqPGroA3HDVqi1 --> address ---> troco but with the amateur attempts not being able to make this recording, my attempt was the following:

// Initialize Firebase (ADD YOUR OWN DATA)
var config = {
  apiKey: "xxxxxxxxxxxxxxxxxxxx",
  authDomain: "xxxxxxxxxxxxxxx",
  databaseURL: "xxxxxxxxxxxxx",
  projectId: "xxxxxxxxxxx",
  storageBucket: "xxxxxxxx",
  messagingSenderId: "xxxxxxxxxxxx"
};
firebase.initializeApp(config);

// Reference messages collection
var messagesRef = firebase.database().ref('users').child(user.uid).child('address');
// Listen for form submit
document.getElementById('trocoForm').addEventListener('submit', submitForm);
// Submit form
function submitForm(e) {
  e.preventDefault();
  // Get values
  var troco = getInputVal('troco');
  // Save message
  saveTroco(troco);
  // Show alert
  // por alert aqui
  // Clear form
  document.getElementById('trocoForm').reset();
}
// Function to get get form values
function getInputVal(id) {
  return document.getElementById(id).value;
}
// Save message to firebase
function saveTroco(troco) {
  var newTrocoRef = messagesRef.push();
  newTrocoRef.set({
    troco_id: troco,
  });
}

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    var user = firebase.auth().currentUser;

    if (user != null) {
      user.providerData.forEach(function(profile) {
        console.log("Sign-in provider: " + profile.providerId);
        console.log("  UID: " + user.uid);
        console.log("  Name: " + profile.displayName);
        console.log("  Email: " + profile.email);
      });
    }
    // User is signed in.
  } else {
    // No user is signed in.
  }
});
<form id="trocoForm">
  <p class="full">
    <label>Informe seu troco</label>
    <input type="tel" pattern="([0-9]{1,3}\.)?[0-9]{1,3},[0-9]{2}$" maxlength="15" name="troco" id="troco"></input>
  </p>
  <p class="full">
    <button type="submit">Submit</button>
  </p> Exemplo 50,00
</form>
<script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>
<script src="troco.js"></script>

logged in user and I ran a script to see if I could recover the user.uid and I could but I can’t write for it, the script used was:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
      var user = firebase.auth().currentUser;

if (user != null) {
  user.providerData.forEach(function (profile) {
    console.log("Sign-in provider: " + profile.providerId);
    console.log("  UID: " + user.uid);
    console.log("  Name: " + profile.displayName);
    console.log("  Email: " + profile.email);
  });
}
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

1 answer

0

This was all that was missing for the project to work:

if (user) {
    uid=user.uid;
  }
});

and edit the line:

var messagesRef = firebase.database().ref('users').child(user.uid).child('address'); 

To:

var messagesRef = firebase.database().ref('users').child(uid); 

And the code went like this:

// Initialize Firebase (ADD YOUR OWN DATA)
var config = {
  apiKey: "xxxxxxxxxxxxxxxx",
  authDomain: "xxxxxxxxxxxxxxxxxxxxxxx",
  databaseURL: "xxxxxxxxxxxxxxxxxxxxx",
  projectId: "xxxxxxxxxxxxxxxxxxxxx",
  storageBucket: "xxxxxxxxxxxxxxxxxxx",
  messagingSenderId: "xxxxxxxxxxxxxxxxxxx"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    uid=user.uid;
    // Reference messages collection
    var messagesRef = firebase.database().ref('users').child(uid);
    // Listen for form submit
    document.getElementById('trocoForm').addEventListener('submit', submitForm);
    // Submit form
    function submitForm(e){
      e.preventDefault();
      // Get values
      var troco = getInputVal('troco');
      // Save message
      saveTroco(troco);
      // Show alert
      // por alert aqui
      // Clear form
      document.getElementById('trocoForm').reset();
    }
    // Function to get get form values
    function getInputVal(id){
      return document.getElementById(id).value;
    }
    // Save message to firebase
    function saveTroco(troco){
      var newTrocoRef = messagesRef.push();
      newTrocoRef.set({
        troco_id: troco,
      });
    }
  }
});

It was missing I make clear to the code who was uid.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.