2017-03-31 11 views
2

複雑で複雑な(コードではない)ファクトリコールを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の呼び出しは、約束は何かを送るずっと前にリターンを得ていることは可能ですバック?もしそうなら、それを回避する私の選択肢は何ですか?

答えて

4

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( 
... 
0

次のような約束のオブジェクトを返す必要があります。

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

あなたはあなたのコードに基づいて、工場出荷時のオブジェクトを返す必要があります。 例...

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; 
      } 
     ); 
    } 
} 
関連する問題