Michael Vasilevsky
asked on
Add Promise to Existing Code
I have the below code that is returning undefined. How to rewrite so it uses a promise or similar mechanism to ensure addTSRecord returns the id to TimesheetRecords?
TimesheetRecords[i].id = addTSRecord(TimesheetRecords[i].RowID, moment(myWE).subtract(6, 'd').toISOString(), TimesheetRecords[i].TSStaffId, TimesheetRecords[i].Hours, TimesheetRecords[i].TSTaskId);
function addTSRecord(myRow, myDate, myTSStaffID, myHours, myTSTaskID) {
var endpointUrl = mySite + "/_api/web/lists/getbytitle('timesheets')/items";
call;
var call = $.ajax({
url: endpointUrl,
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.TimesheetsListItem" },
RowID: myRow,
TSDate: myDate,
TSStaffId: myTSStaffID,
Hours: myHours,
TSTaskId: myTSTaskID
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": myFormDigest
}
});
call.done(function (data, textStatus, jqXHR) {
return data.d.Id;
});
call.fail(function (jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
var message = response ? response.error.message.value : textStatus;
console.log(message);
});
} // addTSRecord
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Julian Hansen, option 2 looks like a good option, but TimesheetRecords.id is being set to an object, not the value of data.d.Id. Is there a way to force just the value?
Appreciate the help!
MV
Appreciate the help!
MV
Looking at your code again I see there is a typo - the promise call back has resp as the parameter yet we are accessing it with data
What is being returned?
If you do this - what do you get?
What is being returned?
If you do this - what do you get?
TimesheetRecords[i].id = addTSRecord(TimesheetRecords[i].RowID, moment(myWE).subtract(6, 'd').toISOString(), TimesheetRecords[i].TSStaffId, TimesheetRecords[i].Hours, TimesheetRecords[i].TSTaskId).then(function(resp) {
console.log(resp);
});
ASKER
Yes needed to replace data with resp.
It appears to be working with the below. Thank you!
It appears to be working with the below. Thank you!
TimesheetRecords[i].id = addTSRecord(TimesheetRecords[i].RowID, moment(myWE).subtract(6, 'd').toISOString(), TimesheetRecords[i].TSStaffId, TimesheetRecords[i].Hours, TimesheetRecords[i].TSTaskId).then(function(resp) {
TimesheetRecords[i - 1].id = resp.d.Id;
});
function addTSRecord(myRow, myDate, myTSStaffID, myHours, myTSTaskID) {
var endpointUrl = mySite + "/_api/web/lists/getbytitle('timesheets')/items";
return $.ajax({
url: endpointUrl,
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.TimesheetsListItem" },
RowID: myRow,
TSDate: myDate,
TSStaffId: myTSStaffID,
Hours: myHours,
TSTaskId: myTSTaskID
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": myFormDigest
}
});
return call;
}
You are welcome.
ASKER
Open in new window