Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用contenteditable更新focusout上的Firebase信息_Javascript_Firebase_Firebase Realtime Database - Fatal编程技术网

Javascript 使用contenteditable更新focusout上的Firebase信息

Javascript 使用contenteditable更新focusout上的Firebase信息,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我一直在尝试更新用户的个人资料信息,他们可以随时更新。我正在显示他们的个人资料信息,并允许他们使用contenteditable更新这些信息,但我似乎无法在Firebase中更新这些信息 基本HTML: <div class="white-container bottom-padding-fortypx container container-about margin-top-eightypx"> <h6 class="margin-top-eightypx">

我一直在尝试更新用户的个人资料信息,他们可以随时更新。我正在显示他们的个人资料信息,并允许他们使用
contenteditable
更新这些信息,但我似乎无法在Firebase中更新这些信息

基本HTML:

<div class="white-container bottom-padding-fortypx container container-about margin-top-eightypx">

    <h6 class="margin-top-eightypx">company</h6>
    <h4 id="companyName" contenteditable="true"></h4>

    <h6>username</h6>
    <h4 id="username"></h4>

 </div>

公司
用户名
JS:

firebase.auth().onAuthStateChanged((user) => {

  var userId = firebase.auth().currentUser.uid;
  firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
    $('#companyName').append('<p>' + snapshot.val().businessName + '</p>');
    $('#username').append('<p>' + snapshot.val().businessAddress + '</p>');
    $('#companyAddress').append('<p>' + snapshot.val().state + '</p>');

    var updatedInfo = document.getElementById('companyName');
    updatedInfo = updatedInfo.innerText;
    console.log(updatedInfo);

    var db = firebase.database();
    $('[contenteditable]').on('keypress focusout', function(en) {
      var currentUser = firebase.auth().currentUser;
      db.ref('users' + currentUser.uid).update({'businessName': updatedInfo});
    })
  });
})
firebase.auth().onAuthStateChanged((用户)=>{
var userId=firebase.auth().currentUser.uid;
firebase.database().ref('/users/'+userId)。一次('value')。然后(函数(快照){
$(“#companyName”).append(“”+snapshot.val().businessName+”

); $(“#用户名”).append(“”+snapshot.val().businessAddress+”

”); $(“#公司地址”).append(“”+snapshot.val().state+”

); var updateInfo=document.getElementById('companyName'); UpdateInfo=UpdateInfo.innerText; console.log(updateInfo); var db=firebase.database(); $(“[contenteditable]”)。在('keypress focusout',函数(en)上{ var currentUser=firebase.auth().currentUser; db.ref('users'+currentUser.uid).update({'businessName':updateInfo}); }) }); })

正如您所看到的,在用户单击
contenteditable
字段后发生
focusout
事件后,我试图更新内容,但是,Firebase中没有更新。有人知道我做错了什么吗

经过一番努力,我找到了解决上述问题的方法(我的HTML没有改变):

Javascript:

document.getElementById('name').addEventListener('focusout', function(e) {
   var updated = document.getElementById('name').value = this.innerText;
   // Getting rid of the new line characters, twice.
   if (updated[updated.length - 1] === '\n')
     updated = updated.slice(0, -2);
   // Pushing the updated profile information to their firebase profile
   db.ref('users/' + userId).update({
     'businessName': updated
   });
});
我在“users”之后添加了一个/和Frank建议的一样,这很有帮助,但使用带有contenteditable的innerTest添加了两个换行符,因此,我使用此代码段删除了以下内容:

if (updated[updated.length - 1] === '\n')
  updated = updated.slice(0, -2);

现在实际值还没有更新为新值,直到我将“.on”更改为“.addEventListener”,并稍微向上修改代码

当我在focusout事件区域中添加console.log时,检查控制台中的值时,focusout看起来仍然显示Firebase中存储的以前的值。在
db.ref('users'+currentUser.uid')中的
'users'
之后缺少
/code>。更新(…
@FrankvanPuffelen,这很有帮助,它确实在Firebase中进行了更新,但我以前使用的名称现在显示相同的名称,但名称为\n\nOk,我添加了一个解决方法以消除所有的\n,但它仍然没有更新新值。