angularfire $添加不起作用

时间:2017-05-12 15:26:22

标签: angularjs firebase ionic-framework firebase-realtime-database angularfire

我正在尝试用离子(v1)和angularfire做一个简单的CRUD。我可以阅读和删除记录,但我需要编辑和创建。实际问题是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所以可能有一些错误。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

好好发现Javascript then是否有任何错误需要回拨一个用于成功,另一个用于失败,如下面的代码

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

现在,对于then,您可以记录返回的错误

答案 1 :(得分: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;

当我们添加用户时,所有工作正常并且模态显示。