非同期コードが実行されるのを待つために js promise を使用しています。期待どおりに動作していますが、ここでの問題は、以下のエラーがスローされることです。
TypeError: PromiseResolve が非オブジェクトで呼び出されました
以下は、aura コンポーネントのセールスフォースで使用されるコントローラーメソッドです。
その他の詳細が必要な場合は、以下にコメントしてください。
前もって感謝します。
init: function (cmp, event, helper) {
setTimeout(function() {
var defaultSortColumn = cmp.get('v.defaultSortColumnNumber');
var defaultSortDirection = cmp.get('v.defaultSortDirection');
$('#Table').DataTable({
retrieve: true,
data: cmp.get('v.data'),
order: [[ defaultSortColumn, defaultSortDirection ]],
oSearch: {"sSearch": cmp.get('v.programName')},
columns: cmp.get('v.columns'),
columnDefs: [{
targets: '_all',
defaultContent: ''
}],
initComplete: function (settings, json) {
cmp.set('v.loaded', true);
$('#Table tbody').on('click', 'a.updatePharmacy', function (e) {
helper.navigateToCommunityPage(cmp, e, 'Pharmacy');
});
$('#Table tbody').on('click', 'a.update', function (e) {
helper.navigateToCommunityPage(cmp, e, 'Pat');
});
$('#Table tbody').on('click', 'a.discontinue', function (e) {
helper.handleDiscontinue(cmp, e);
});
$('#Table tbody').on('click', 'a.confirm', function (e) {
helper.reactivate(cmp, e);
});
$('#Table tbody').on('click', 'a.remove', function (e) {
var params = { accountId : e.currentTarget.dataset.id};
cmp.set('v.loaded', false);
helper.handleRemove(cmp, e,params)
.then($A.getCallback(function(r) {
console.log('First From Server-Side: ' + r);
}))
.catch(function(errors) {
console.error('ERROR: ' + errors);
});
});
}
});
}, 100);
},
handleRemove : function (cmp, event,params) {
return new Promise($A.getCallback(function(resolve, reject) {
var action = cmp.get('c.removeFavorite');
var recId = event.currentTarget.dataset.id;
action.setParams({accId:recId});
action.setCallback(this, function(response){
var state = response.getState();
cmp.set('v.loaded', true);
if (state === "SUCCESS"){
console.log('success');
resolve({'c':cmp, 'r':response.getReturnValue()});
$A.get('e.force:refreshView').fire();
}
else if (state === "INCOMPLETE") {
reject(action.getError())
console.log('incomplete ' + response);
}
else {
reject(action.getError());
var errors = response.getError();
var error = $A.get('$Label.c.AZ_Error');
var errorMessage = 'Could not remove account';
var toastParams = {
title: error,
message: errorMessage,
type: "error"
};
if (errors && Array.isArray(errors) && errors.length > 0) {
toastParams.message = errors[0].message;
console.log('errors ' + toastParams.message);
}
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams(toastParams);
toastEvent.fire();
}
});
$A.enqueueAction(action);
}));
}