【问题标题】:Call to Factory-defined method returning undefined value/promise调用返回未定义值/承诺的工厂定义方法
【发布时间】:2017-03-31 19:13:39
【问题描述】:

我在 Angular 中有一个错综复杂的(不是我的代码)工厂调用,当使用它时,它不会返回 then 属性或任何可用于运行 successCallback 方法的东西(错误本身是 TypeError: Cannot read property 'then' of undefined)。我不确定原因可能是什么,但调用的许多独特组件可能是原因,例如对 Web 服务的多个嵌套 $http.post 调用。

    updateDocument: function(documentId,  newFileData, appointmentFileName = null, appointmentCategory = null, appointmentId = null) {
        //Get Existing Document Details (including Revision)
        //document.
        documentsService.documentsFactory.getDocumentRecordById(documentId).then(
            function successCallback(response) {
                console.debug("Response", response);

                var NextRevision = parseInt(response.data.revision) + 1;

                if ((appointmentCategory === null) || (appointmentCategory === undefined)) {
                    appointmentCategory = response.data.category_id;
                }

                if ((appointmentId === null) || (appointmentId === undefined)) {
                    var ErrorObj = {
                        status: 10,
                        message: 'Appointment ID not defined.'
                    };
                    return ErrorObj;
                }

                if ((appointmentFileName === null) || (appointmentFileName === undefined)) {
                    appointmentFileName = response.data.filename;
                }

                if ((newFileData === null) || (newFileData === undefined)) {
                    var ErrorObj = {
                        status: 11,
                        message: 'File Data not defined.'
                    };
                    return ErrorObj;
                }

                var action = 'set_document_revision';
                var endpoint = cfg.url;
                var sessionId = systemService.sessionService.getSessionId();

                var DocRevObj = {
                    session: sessionId, 
                    document_revision: {
                        id: documentId,
                        file: newFileData,
                        filename: appointmentFileName,
                        revision: NextRevision                        
                    }
                };

                var DocNodeObj = {
                    session: sessionId,
                    module: "Documents",
                    name_value_list: [{
                        name: 'document_name',
                        value: appointmentFileName
                    }, {
                        name: 'category_id',
                        value: appointmentCategory
                    }, {
                        name: 'id',
                        value: documentId
                    }]                      
                };

                var RevisionRequestParams = {
                    method: action,
                    input_type: "JSON",
                    response_type: "JSON",
                    rest_data: DocRevObj
                };

                var NodeRequestParams = {
                    method: "set_entry",
                    input_type: "JSON",
                    response_type: "JSON",
                    rest_data: DocNodeObj
                }

                var headers = {
                    "Content-Type": "application/json"
                };

                return $http.post(endpoint, RevisionRequestParams, headers).then(
                    function successCallback(response2) {
                        console.debug("Successfully Replaced File", response2);

                        //Re-adjust the File Entry to match new changes
                        //(make a call to set_entry)
                        return $http.post(endpoint, NodeRequestParams, headers).then(
                            function successCallback(response3) {
                                console.debug("Successfully Updated File", response3);
                                return response3;
                            },
                            function errorCallback(response3) {
                                console.debug("Error", response3);
                                return response3
                            }
                        );

                        return response2;
                    },
                    function errorCallback(response2) {
                        console.debug("Error", response2);
                        return response2;
                    }
                ); 
                console.debug("Success", response);
                return response;
            }, function errorCallback(response) {
                console.debug("Error", response);
                return response;
            }
        );
    }

引用方法调用(在控制器内部,在点击事件时触发)

appointmentsService.appointmentsFactory.updateDocument(CurrentDocumentId, result, NewFileName, NewDocumentType, CurrentAppointmentID).then(
                    function successCallback(response) {
                        //Success Callback Logic                        
                    },
                    function errorCallback(response) {

                    });

事实上,updateDocument 的调用是否有可能在 Promise 发送回任何东西之前很久就得到了返回?如果是这样,我有哪些解决方法?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    您的 updateDocument 函数不返回任何内容。在 getDocumentRecordById 调用之前添加 return。

    var updateDocument = function(documentId, newFileData, appointmentFileName = null, appointmentCategory = null, appointmentId = null) {
    //Get Existing Document Details (including Revision)
    //document.
    return documentsService.documentsFactory.getDocumentRecordById(documentId).then( 
    ...
    

    【讨论】:

      【解决方案2】:

      你需要像这样返回promise对象:

      updateDocument: function(documentId,  newFileData, appointmentFileName = null, appointmentCategory = null, appointmentId = null) {
          //Get Existing Document Details (including Revision)
          //document.
          return documentsService.documentsFactory.getDocumentRecordById(documentId).then(
              function successCallback(response) {
                  console.debug("Response", response);
      
                  var NextRevision = parseInt(response.data.revision) + 1;
      
                  if ((appointmentCategory === null) || (appointmentCategory === undefined)) {
                      appointmentCategory = response.data.category_id;
                  }
      
                  if ((appointmentId === null) || (appointmentId === undefined)) {
                      var ErrorObj = {
                          status: 10,
                          message: 'Appointment ID not defined.'
                      };
                      return ErrorObj;
                  }
      
                  if ((appointmentFileName === null) || (appointmentFileName === undefined)) {
                      appointmentFileName = response.data.filename;
                  }
      
                  if ((newFileData === null) || (newFileData === undefined)) {
                      var ErrorObj = {
                          status: 11,
                          message: 'File Data not defined.'
                      };
                      return ErrorObj;
                  }
      
                  var action = 'set_document_revision';
                  var endpoint = cfg.url;
                  var sessionId = systemService.sessionService.getSessionId();
      
                  var DocRevObj = {
                      session: sessionId, 
                      document_revision: {
                          id: documentId,
                          file: newFileData,
                          filename: appointmentFileName,
                          revision: NextRevision                        
                      }
                  };
      
                  var DocNodeObj = {
                      session: sessionId,
                      module: "Documents",
                      name_value_list: [{
                          name: 'document_name',
                          value: appointmentFileName
                      }, {
                          name: 'category_id',
                          value: appointmentCategory
                      }, {
                          name: 'id',
                          value: documentId
                      }]                      
                  };
      
                  var RevisionRequestParams = {
                      method: action,
                      input_type: "JSON",
                      response_type: "JSON",
                      rest_data: DocRevObj
                  };
      
                  var NodeRequestParams = {
                      method: "set_entry",
                      input_type: "JSON",
                      response_type: "JSON",
                      rest_data: DocNodeObj
                  }
      
                  var headers = {
                      "Content-Type": "application/json"
                  };
      
                  return $http.post(endpoint, RevisionRequestParams, headers).then(
                      function successCallback(response2) {
                          console.debug("Successfully Replaced File", response2);
      
                          //Re-adjust the File Entry to match new changes
                          //(make a call to set_entry)
                          return $http.post(endpoint, NodeRequestParams, headers).then(
                              function successCallback(response3) {
                                  console.debug("Successfully Updated File", response3);
                                  return response3;
                              },
                              function errorCallback(response3) {
                                  console.debug("Error", response3);
                                  return response3
                              }
                          );
      
                          return response2;
                      },
                      function errorCallback(response2) {
                          console.debug("Error", response2);
                          return response2;
                      }
                  ); 
                  console.debug("Success", response);
                  return response;
              }, function errorCallback(response) {
                  console.debug("Error", response);
                  return response;
              }
          );
      }
      

      【讨论】:

        【解决方案3】:

        您应该根据您的代码返回工厂对象。 示例...

         app.factory('factoryName', function() {
             var factoryObj= { 
              save: function() {
              },
            update: function() {
            }
          };
           return factoryObj; //return object
         });
        

        您可以返回您的代码,如下所示。

        return {
        updateDocument: function(documentId,  newFileData, appointmentFileName = null, appointmentCategory = null, appointmentId = null) {
                //Get Existing Document Details (including Revision)
                //document.
                documentsService.documentsFactory.getDocumentRecordById(documentId).then(
                    function successCallback(response) {
                        console.debug("Response", response);
        
                        var NextRevision = parseInt(response.data.revision) + 1;
        
                        if ((appointmentCategory === null) || (appointmentCategory === undefined)) {
                            appointmentCategory = response.data.category_id;
                        }
        
                        if ((appointmentId === null) || (appointmentId === undefined)) {
                            var ErrorObj = {
                                status: 10,
                                message: 'Appointment ID not defined.'
                            };
                            return ErrorObj;
                        }
        
                        if ((appointmentFileName === null) || (appointmentFileName === undefined)) {
                            appointmentFileName = response.data.filename;
                        }
        
                        if ((newFileData === null) || (newFileData === undefined)) {
                            var ErrorObj = {
                                status: 11,
                                message: 'File Data not defined.'
                            };
                            return ErrorObj;
                        }
        
                        var action = 'set_document_revision';
                        var endpoint = cfg.url;
                        var sessionId = systemService.sessionService.getSessionId();
        
                        var DocRevObj = {
                            session: sessionId, 
                            document_revision: {
                                id: documentId,
                                file: newFileData,
                                filename: appointmentFileName,
                                revision: NextRevision                        
                            }
                        };
        
                        var DocNodeObj = {
                            session: sessionId,
                            module: "Documents",
                            name_value_list: [{
                                name: 'document_name',
                                value: appointmentFileName
                            }, {
                                name: 'category_id',
                                value: appointmentCategory
                            }, {
                                name: 'id',
                                value: documentId
                            }]                      
                        };
        
                        var RevisionRequestParams = {
                            method: action,
                            input_type: "JSON",
                            response_type: "JSON",
                            rest_data: DocRevObj
                        };
        
                        var NodeRequestParams = {
                            method: "set_entry",
                            input_type: "JSON",
                            response_type: "JSON",
                            rest_data: DocNodeObj
                        }
        
                        var headers = {
                            "Content-Type": "application/json"
                        };
        
                        return $http.post(endpoint, RevisionRequestParams, headers).then(
                            function successCallback(response2) {
                                console.debug("Successfully Replaced File", response2);
        
                                //Re-adjust the File Entry to match new changes
                                //(make a call to set_entry)
                                return $http.post(endpoint, NodeRequestParams, headers).then(
                                    function successCallback(response3) {
                                        console.debug("Successfully Updated File", response3);
                                        return response3;
                                    },
                                    function errorCallback(response3) {
                                        console.debug("Error", response3);
                                        return response3
                                    }
                                );
        
                                return response2;
                            },
                            function errorCallback(response2) {
                                console.debug("Error", response2);
                                return response2;
                            }
                        ); 
                        console.debug("Success", response);
                        return response;
                    }, function errorCallback(response) {
                        console.debug("Error", response);
                        return response;
                    }
                );
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-10
          • 1970-01-01
          相关资源
          最近更新 更多