【问题标题】:angularfire $add not workingangularfire $add 不工作
【发布时间】:2017-10-11 23:43:56
【问题描述】:

我正在尝试使用 ionic (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,所以可能有一些错误。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    找出是否有任何错误 Javascript then 需要回调一个成功,另一个回调失败,如下面的代码

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

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

    【讨论】:

    • 错误是“错误:密钥 id 未定义。无法在 JSON 中传递未定义。请改用 null。”
    【解决方案2】:

    错误已修复。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多