【问题标题】:TypeError : callback is not a functionTypeError:回调不是函数
【发布时间】:2018-06-05 00:44:34
【问题描述】:

只有当我尝试添加一个新的时,我才得到一个回调不是一个函数 参数 erreur=TypeError:回调不是函数 在 services.js:72 我尝试添加对函数的引用,当我移动 evrything 工作正常这是我在 service.js 的代码我希望你能帮助我。

    .factory('Payement', function ($http, $ionicLoading) {
            return {
                saveContratEtudant: function (_nb_piece1,_nb_piece2,_shortadresse,_govClt,_villeClt,_localiteClt,_voieClt,_cod_logem,_adresse_logem,_govLogem,_gelgLogem, _dateDel,_dateEffet,_dateExp,_nomAssure, _prenomAssure ,_piece2,_numDocument,_typeContrat,_natureContrat,_piece1,_dateNais,_email,_phone,_IdTrans,_reference,_numDocuemnt,_codePostal,_adresse,_typedocu,_sexeClt,_tarif,_frais ,_tva, _tcc, callback) {

                    $http({
                        method: 'POST',
                        url:SAVE_ETUDIANT,
                        timeout: 10000,
                        data: {
                            nb_piece1:_nb_piece1,
                            nb_piece2:_nb_piece2,
                            shortadresse:_shortadresse,
                            govClt:_govClt,
                            villeClt:_villeClt,
                            localiteClt:_localiteClt,
                            voieClt:_voieClt,
                            cod_logem:_cod_logem,
                            adresse_logem:_adresse_logem,
                            govLogem:_govLogem,
                            gelgLogem:_gelgLogem,
                            dateDelivra:_dateDel,
                            dateEffet:_dateEffet,
                            dateExp:_dateExp,
                            nomAssure:_nomAssure,
                            prenomAssure:_prenomAssure,
                            piece2:_piece2,
                            numDocument:_numDocument,
                            typeContrat:_typeContrat,
                            natureContrat:_natureContrat,
                            piece1:_piece1,
                            dateNais:_dateNais,
                            email:_email,
                            phone:_phone,
                            IdTrans: _IdTrans,
                            reference:_reference
                            numDocuemnt:_numDocuemnt,
                            codePostal:_codePostal,
                            adresse:_adresse,
                            typedocu:_typedocu,
                            sexeClt:_sexeClt,
                            //nbr_piece: ,
                            tarif : _tarif ,
                            frais : _frais,
                            tva :_tva,
                            tcc :_tcc,
                            //reference :_reference


                        },
                    }).then(function (response) {
                        callback(response);
                       // $ionicLoading.hide();
                        console.log("ajout ");
                    });
                }
            };
        })

这是我在 controller.js 上的代码,我最近尝试向我的 BD 添加一个引用,所以在我的控制器中我设置了一个 var ref,然后我在控制台中得到了 var 显示

sendbtn.addEventListener('click', function () {
            Payement.saveContratEtudant($scope.nb_piece1,$scope.nb_piece2,$scope.shortadresse,$scope.govClt,$scope.villeClt,localiteClt,$scope.voieClt,$scope.cod_logem,$scope.adresse_logem,$scope.govLogem,$scope.gelgLogem,$scope.dateDelivra, $scope.dateEffet, $scope.dateExp, $scope.nomAssure,$scope.prenomAssure,$scope.piece2,$scope.numDocument,$scope.typeContrat,$scope.natureContrat,$scope.piece1, $scope.dateNais,$scope.email,$scope.phone, $scope.IdTrans,$scope.reference, $scope.numDocuemnt, $scope.codePostal, $scope.adresse,$scope.typedocu,$scope.sexeClt,$scope.tarif,$scope.frais,$scope.tva,$scope.tcc ,function (data) {
               console.log("test saveContrat")
                //console.log("date:",date)
                if (data.data.state_code == 200){
                    $scope.local_ = data;
                    console.log($scope.local_.data.data);
                    $state.go('map')
                } else {
                     $ionicPopup.alert({
                        title: "Alert",
                        template: data.data.message
                    });
                }
            });

这是确切的错误 TypeError: callback is not a function

  at services.js:72
    at processQueue (ionic.bundle.js:29132)
    at ionic.bundle.js:29148
    at Scope.$eval (ionic.bundle.js:30400)
    at Scope.$digest (ionic.bundle.js:30216)
    at Scope.$apply (ionic.bundle.js:30508)
    at done (ionic.bundle.js:24829)
    at completeRequest (ionic.bundle.js:25027)
    at XMLHttpRequest.requestLoaded (ionic.bundle.js:24968) 

【问题讨论】:

  • 添加更多代码,因为您的函数callback() 没有在任何地方定义。
  • function callback() { if (!hasParams) { fn(iteration); } else { fn.apply(null, args); }
  • 我的问题是该错误仅在我尝试添加新参数时显示?!
  • 你不应该尝试添加一个参数,而是像这样合并它们:saveContratEtudant: function (allArgsInASignleObject, callback)
  • 每次添加参数时,都必须更改 3 件事:saveContratEtudant 函数、用于在事件处理程序中调用 saveContratEtudant 的参数和数据对象。如果您要在事件处理程序中创建数据对象并传递它,您可以向对象添加任意数量的参数,而无需更改函数或函数调用。

标签: javascript angularjs node.js ionic-framework


【解决方案1】:

这只是意味着在这部分代码中:

.then(function (response) {
    callback(response);  // <- HERE
    // $ionicLoading.hide();
    console.log("ajout ");
});

您正在调用名为“回调”的函数。但它没有定义。可能是因为这个函数的最后一个参数没有作为参数传递:

saveContratEtudant: function (_nb_piece1,_nb_piece2,_shortadresse,_govClt,_villeClt,_localiteClt,_voieClt,_cod_logem,_adresse_logem,_govLogem,_gelgLogem, _dateDel,_dateEffet,_dateExp,_nomAssure, _prenomAssure ,_piece2,_numDocument,_typeContrat,_natureContrat,_piece1,_dateNais,_email,_phone,_IdTrans,_reference,_numDocuemnt,_codePostal,_adresse,_typedocu,_sexeClt,_tarif,_frais ,_tva, _tcc, callback) {

类似的分割线:

saveContratEtudant: function (_nb_piece1,
                              _nb_piece2,
                              _shortadresse,
                              _govClt,
                              _villeClt,
                              _localiteClt,
                              _voieClt,
                              _cod_logem,
                              _adresse_logem,
                              _govLogem,
                              _gelgLogem, _dateDel,
                              _dateEffet,
                              _dateExp,
                              _nomAssure,
                              _prenomAssure ,
                              _piece2,
                              _numDocument,
                              _typeContrat,
                              _natureContrat,
                              _piece1,
                              _dateNais,
                              _email,
                              _phone,
                              _IdTrans,
                              _reference,
                              _numDocuemnt,
                              _codePostal,
                              _adresse,
                              _typedocu,
                              _sexeClt,
                              _tarif,
                              _frais,
                              _tva,
                              _tcc,
                              callback // <- HERE
                              ) {

您应该在调用它之前检查它是否已定义。或者检查您的代码是否提供了真正的功能。

专业提示:你不应该在函数中有那么多参数。

所以你应该这样尝试:

    .factory('Payement', function ($http, $ionicLoading) {
        return {
            saveContratEtudant: function (params, callback) {
                $http({
                    method: 'POST',
                    url:SAVE_ETUDIANT,
                    timeout: 10000,
                    data: params,
                }).then(function (response) {
                    callback(response);
                   // $ionicLoading.hide();
                    console.log("ajout ");
                });
            }
        };
    })

然后这样称呼它:

Payement.saveContratEtudant($scope, function (data) {...})

【讨论】:

  • 它应该仍然在范围内,除非由于某种原因saveContratEtudant 没有创建闭包。它需要 36 个参数,除非我数错,否则会传递 36 个参数,所以问题出在其他地方。 :x 我完全同意,一旦你必须传递超过 +- 6 个参数,一个对象和一个回调会更好。所以数据对象应该在事件监听器中创建,saveContratEtudant 应该像函数(数据,回调):)
  • 该错误仅在我尝试添加新参数(_reference)时显示!我要把它们作为一个对象发送吗?
  • 所以你的参数数量有问题。请将它们合并为一个,它将更易于维护。并且更兼容 ES 6/7/8 规范。您需要样品吗?
  • 看我的编辑。如果它解决了您的问题,请不要忘记标记为“有效”;)
猜你喜欢
  • 2018-01-22
  • 2017-03-28
  • 2016-10-25
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多