未捕获的TypeError:无法读取未定义的属性“0”

时间:2017-04-20 13:10:56

标签: javascript firebase firebase-realtime-database firebase-storage

我有以下功能;我想将表单的条目保存到firebase数据库。该函数尝试将上传的文件保存到firebase存储并下载文件URL。然后将文件URL添加到要保存到firebase基础数据库的其他用户数据中。所以我有两个问题;这是同时保存到firebase存储和数据库的最佳方法,我也有错误“firebase.js:88 Uncaught TypeError:无法读取未定义的属性'0'

     var file = event.target.files[0];

我做错了什么,我该如何纠正?

 <button ng-disabled="form-register.$invalid" class="waves-effect waves-light btn done"  onclick="newentry();">
           <i class="material-icons left" >done</i> Register
           </button>

上面的按钮调用newentry函数

function newentry() {

      var database = firebase.database();
      var fname = document.getElementById('first-name').value;
      var lname = document.getElementById('last-name').value;
      var businessname = document.getElementById('business-name').value; 
      var email = document.getElementById('email').value;
      var password = document.getElementById('password').value;
      var industry = document.getElementById('industry').value;
       //Get Elements


        if (email.length < 4) {
        alert('Please enter an email address.');
        return;
      }
      if (password.length < 4) {
        alert('Please enter a password.');
        return;
      }

      firebase.auth().createUserWithEmailAndPassword(email, password) .then(function(user) {
           var root = firebase.database().ref();
           var uid = user.uid;
           var auth = firebase.auth();
           var storageRef = firebase.storage().ref();

           // Store image in firebase storage 

                event.stopPropagation();
                event.preventDefault();
                var file = event.target.files[0];

                var metadata = {
                  'contentType': file.type
                };

                // Push to child path.
                // [START oncomplete]
                storageRef.child('images/' + file.name).put(file, metadata).then(function(snapshot) {
                  console.log('Uploaded', snapshot.totalBytes, 'bytes.');
                  console.log(snapshot.metadata);
                  var url = snapshot.downloadURL;
                  console.log('File available at', url);
                  // [START_EXCLUDE]
                  document.getElementById('linkbox').innerHTML = '<a href="' +  url + '">Click For File</a>';
                  // [END_EXCLUDE]
                }).catch(function(error) {
                  // [START onfailure]
                  console.error('Upload failed:', error);
                  // [END onfailure]
                });
                // [END oncomplete]

           var postData = {
             BusinessName: businessname,
             email: email,
             profileImageUrl: url,
             Category: industry,
           };



           var updates = {};

          updates['/BusinessCategories/' + industry + "/" + uid ] = postData;
          updates['/Businesses/' + uid] = postData;



            return firebase.database().ref().update(updates); 

        })

0 个答案:

没有答案