错误:Firebase.set失败:第一个参数在属性'achv_img'中包含未定义

时间:2015-08-13 08:32:56

标签: javascript angularjs file-upload firebase uploader

我关注this post

使用angular和firebase achievementsapp.firebaseapp.com进行表单,你可以注册,然后如果你点击绿色加它应该弹出表单,上传按钮不起作用,当点击添加按钮它给我这个错误< strong>错误:Firebase.set失败:第一个参数在属性'achv_img'中包含未定义

控制器代码:

myApp.controller('MeetingsController',
  function($scope, $rootScope, $firebase, Uploader,
    CountMeetings, FIREBASE_URL) {

  var ref = new Firebase(FIREBASE_URL + '/users/' + 
    $rootScope.currentUser.$id + '/meetings');

  var meetingsInfo = $firebase(ref);
  var meetingsObj = meetingsInfo.$asObject();

  meetingsObj.$loaded().then(function(data) {
    $scope.meetings = data;
  }); //make sure meetings data is loaded

  $scope.addMeeting = function() {
    meetingsInfo.$push({
      name: $scope.meetingname,
      description: $scope.meetingdescription,
      achv_type: $scope.achvtype,
      achv_date: $scope.achvdate,
      achv_img : $scope.achvimg,
      date: Firebase.ServerValue.TIMESTAMP
    }).then(function() {
      $scope.meetingname='';
      $scope.achvtype = '';
      $scope.meetingdescription='';
      $scope.achvdate='';
      $scope.achvimg= $scope.meetingsInfoImgData;
    });
    Uploader.create($scope.meetingsInfo);


  $scope.handleFileSelectAdd = function(evt) {
    var f = evt.target.files[0];
    var reader = new FileReader();
    reader.onload = (function(theFile) {
      return function(e) {
        var filePayload = e.target.result;
        $scope.meetingsInfoImgData = e.target.result; 
        document.getElementById('pano').src = $scope.meetingsInfoImgData; 
      };
    })(f);
    reader.readAsDataURL(f);
  };
  document.getElementById('file-upload').addEventListener('change', $scope.handleFileSelectAdd, false);


  }; //addmeeting

  $scope.deleteMeeting = function(key) {
    meetingsInfo.$remove(key);
  }; //deleteMeeting

}); //MeetingsController

2 个答案:

答案 0 :(得分:3)

reader.onload()中,您从文件中获取图像数据并将其放入$scope.meetingsInfoImgData

reader.onload = (function(theFile) {
  return function(e) {
    var filePayload = e.target.result;
    $scope.meetingsInfoImgData = e.target.result; 
    document.getElementById('pano').src = $scope.meetingsInfoImgData; 
  };
})(f);

我的原始答案稍后会使用它的等效$scope.meetingsInfoImgData来填充它发送给Firebase的JavaScript对象:

$scope.episode.img1 = $scope.episodeImgData;
var episodes = $firebase(ref).$asArray();
episodes.$add($scope.episode);

尝试在Firebase中创建对象时,您的代码对$scope.meetingsInfoImgData不执行任何操作。您的代码中可能还需要类似于$scope.episode.img1 = $scope.episodeImgData;的内容。

答案 1 :(得分:1)

如果由于某种原因你无法在源头修复它,为了确保你的对象不包含任何未定义的道具,请使用这个简单的技巧: JSON.parse( JSON.stringify(ObjectToSave ) )

有关详细信息,请查看此代码图:http://codepen.io/ajmueller/pen/gLaBLX