angularfire $add 不工作

angularfire $add not working

我正在尝试使用 ionic (v1) 和 angularfire 做一个简单的 CRUD。我可以读取和删除记录,但我需要编辑和创建。实际的问题是 angularfire 函数 $add`,这个函数什么都不做,控制台也没有 return 任何错误。我有这个代码:

    $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);;
        })

显然代码没问题,但 return console.log 不行,所以可能有一些错误。有什么想法吗?

好吧,看看是否有任何错误 Javascript then 需要回调一个成功,另一个失败,类似于下面的代码

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

现在是 then 这样您就可以记录返回的错误

错误已修复。

$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;

这样一来,一切正常,当我们添加用户时会显示模态。