我在Angular中有一个错综复杂的(不是我的代码)工厂调用,使用时不会返回一个then属性或任何可用于运行successCallback的属性方法(错误本身是TypeError: Cannot read property 'then' of undefined
)。我不确定原因可能是什么,但是可能是原因的调用有很多独特的组件,例如对Web Service的多个嵌套的$ http.post调用。调用工厂定义的方法返回undefined值/ promise
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是得到一个回报的承诺发送任何没过多久背部?如果是这样,我有什么选择来解决它?