Javascript 调用工厂定义的方法,返回未定义的值/承诺

Javascript 调用工厂定义的方法,返回未定义的值/承诺,javascript,angularjs,Javascript,Angularjs,我有一个复杂复杂的(不是代码)的工厂调用,在使用时,它不会返回一个then属性或任何可用于运行successCallback方法的东西(错误本身是TypeError:无法读取未定义的属性'then'。我不确定原因是什么,但调用中有很多独特的组件可能是原因,例如对Web服务的多个嵌套$http.post调用 updateDocument: function(documentId, newFileData, appointmentFileName = null, appointmentCa

我有一个复杂复杂的(不是代码)的工厂调用,在使用时,它不会返回一个then属性或任何可用于运行successCallback方法的东西(错误本身是
TypeError:无法读取未定义的属性'then'。我不确定原因是什么,但调用中有很多独特的组件可能是原因,例如对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;
            }
        );
    }
引用方法调用(在控制器内,在单击事件时触发)


事实上,要求更新文档的请求是否可能在承诺发送任何内容之前很久就得到回报?如果是这样的话,我有什么办法来解决这个问题?

您的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( 
...

您需要像下面这样返回承诺对象:

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

您应该根据代码返回factory对象。 例如

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