如何使用CurrentUserId在Firebase中执行CRUD操作? [英] How to do CRUD operations in Firebase with CurrentUserId?

查看:113
本文介绍了如何使用CurrentUserId在Firebase中执行CRUD操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的网站,并且无法使用带有currentUserId的firebase实时数据库进行操作。

I'm creating a simple website and i'cant use firebase realtime databases crud operations with currentUserId.

我的身份验证系统运行良好;我可以在数据库上登录/注册/注销。而且我可以通过从表单获取输入将数据写入Firebase。

My auth system working good; i can signIn/signUp/signOut on database. And i can write data to firebase with getting inputs from form.

    //GETTING DATA WITH FORM
    
    // Listen for form submit
    document.getElementById('form').addEventListener('submit', submitForm);
    
    
    // Submit form
    function submitForm(e){
      e.preventDefault();
    
      // Get values
      var name = getInputVal('name');
      var sets = getInputVal('sets');
      var reps = getInputVal('reps');
      var weights = getInputVal('weights');
      
      // Write data
      writeData(name, sets, reps, weights);
    
      // Clear form
      document.getElementById('form').reset();
    }
    
    
    // Function to get form values
    function getInputVal(id){
      return document.getElementById(id).value;
    }
    
    
    //CRUD OPERATIONS
    
    // Reference
    var DataRef = firebase.database().ref('users/'  + 'exercises/');
    
    // Write data to database
    function writeData(name, sets, reps, weights){
      var newDataRef = DataRef.push();
      newDataRef.set({
    
        name: name,
        sets: sets,
        reps: reps,
        weights: weights,
        
      });
    }

我不想开始阅读,更新和删除直到我解决了这个id问题。

I didn't want to start reading, updating and deleting until I solved this id problem.

那么如何对具有id的每个用户执行CRUD操作?我该怎么办?

So how to do CRUD operations with each users with id? What can i do?

推荐答案

您是否要问如何在当前用户特定的位置写入数据?如果是这样:

Are you asking how to write data in a location that is specific to the current user? If so:

var currentUser = firebase.auth().currentUser;
var DataRef = firebase.database().ref('users/' + currentUser.uid + '/exercises/');

请注意,这仅在保证当前用户已经登录的情况下才有效。请确保将上面的代码放在 auth状态监听器

Note that this only works if the current user is guaranteed to be already signed in. If you can't guarantee that, put the above code in an auth state listener.

这篇关于如何使用CurrentUserId在Firebase中执行CRUD操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆