I am trying to get the body of my outlook mail using javascript. Currently i am able to extract title but not body.
document.getElementById("fill-data").onclick = function () {
var item = Office.context.mailbox.item;
var bodyContent;
Office.context.mailbox.item.body.getAsync('text', function (async) {bodyContent = async.value});
document.getElementById("txt-subject").value = item.subject;
// The below code returns "undefined"
document.getElementById("txt-description").value = bodyContent;
};
Office.context.mailbox.item.body.getAsync('text', function (async) {bodyContent = async.value}); is not returning body instead it returns "undefined" into my textbox .How can i extract body of outlook mail?
CodePudding user response:
Office.context.mailbox.item.body.getAsync(
'text',
function (async) {bodyContent = async.value}
);
sets the value of bodyContent
in its callback. The callback is invoked asynchronously (i.e. the call to Office.context.mailbox.item.body.getAsync
does not block).
This means that by the time you
document.getElementById("txt-description").value = bodyContent;
that callback function has not yet been invoked and bodyContent
has not been assigned to
Don't set the value
until the callback has been invoked. How? Just doing it in the callback would be one way.
Office.context.mailbox.item.body.getAsync(
'text',
function (result) {
document.getElementById("txt-description").value = result.value;
}
);
Side-note: I'd strongly recommend against the variable-name async
, even if your environment allows it.