1

我試圖做一個簡單的CRUD與離子(v1)和angularfire。我可以閱讀和刪除記錄,但我需要編輯和創建。實際的問題是angularfire函數$ add`,這個函數不會執行任何操作,並且不會在控制檯中返回任何錯誤。我有這樣的代碼:

$scope.users = $firebaseArray(root.ref('/users/')); 
    $scope.user = {}; 
    //Create 
    $scope.title = "New user" 
    $scope.button = "Create"; 
    $scope.icon = "ion-person-add"; 
    $scope.submit = function() { 
     var data = { 
      name: $scope.user.name, 
      username: $scope.user.username, 
      email: $scope.user.email, 
      street: $scope.user.street, 
      suite: $scope.user.suite, 
      city: $scope.user.city, 
      lat: $scope.user.lat, 
      lng: $scope.user.lng, 
      phone: $scope.user.phone, 
      website: $scope.user.website, 
     } 
     console.log(data); 
     $scope.users.$add(data).then(function (ref) { 
      console.log('contact added with Id: ' + id);; 
     }) 

顯然,代碼是好的,但不返回console.log所以也許有一些錯誤。有任何想法嗎?

回答

0

修正了錯誤。

$scope.users = $firebaseArray(root.ref('/users/')); 
      $scope.user = {}; 
      //Create 
      $scope.title = "New user" 
      $scope.button = "Create"; 
      $scope.icon = "ion-person-add"; 
      $scope.submit = function() { 
       var data = { 
        name: $scope.user.name, 
        phone: $scope.user.phone, 
        email: $scope.user.email, 
        username: $scope.user.username, 
        city: $scope.user.city, 
        lat: $scope.user.lat, 
        lng: $scope.user.lng, 
        street: $scope.user.street, 
        suite: $scope.user.suite, 
        website: $scope.user.website, 
        zipcode: $scope.user.zipcode, 
       } 
       $scope.users.$add(data).then(function (response) { 
        if (response) { 
         $ionicPopup.alert({ 
          title: '<div><i class="ion-checkmark-circled"></i></div>', 
          subTitle: '<h4>The user <b>' + data.name + '</b> is added.</h4>', 
         }); 
        } 
       $scope.user=null; 

隨着所有工作正常和模式顯示,當我們添加用戶。

0

嘛,看看是否有任何錯誤的Javascript then需要再打一個成功,另一個用於故障類似下面的代碼

$scope.user.$add(data).then(function (success){ 
return console.log(success); 
}, function(error){ 
return console.log(error); 
}) 

現在,這是爲then這樣你可以登錄返回的錯誤

+0

錯誤是「錯誤:密鑰ID未定義,無法在JSON中傳遞未定義,請改爲使用null」。 –