我正在使用 js 承诺等待异步代码执行。它按预期工作,但这里的问题是它抛出错误。
TypeError: PromiseResolve 在非对象上调用
下面是 aura 组件 salesforce 中使用的控制器方法。
如果需要任何其他详细信息,请在下方评论。
提前致谢。
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);
}));
}