所以我在关注 tutorial to build an Outlook Add-in .但是,该演示不显示消息的 body
。
我还从文档中了解到,我可以调用 getAsync
来访问正文,但它不起作用。我需要在这里使用 async await
吗?
代码如下:
function loadProps() {
$("#attachments").html(buildAttachmentsString(item.attachments));
$("#cc").html(buildEmailAddressesString(item.cc));
$("#conversationId").text(item.conversationId);
$("#from").html(buildEmailAddressString(item.from));
$("#internetMessageId").text(item.internetMessageId);
$("#normalizedSubject").text(item.normalizedSubject);
$("#sender").html(buildEmailAddressString(item.sender));
$("#subject").text(item.subject);
$("#to").html(buildEmailAddressesString(item.to));
$("#body").text(buildEmailBodyString()); //async function
}
function buildEmailBodyString() {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
return resText.value;
});
}
最佳答案
您的问题是您的 buildEmailBodyString
触发了 getAsync
并立即存在。它不会从函数返回 restText.value
,因为该函数已经存在。
function buildEmailBodyString() {
// 1. Fires function
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
// 3. returns a value to nothing
return resText.value;
});
// 2. Exits function
}
这里的一个解决方案是在回调中设置 $("#body")
:
function buildEmailBodyString() {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
$("#body").text(resText.value);
});
}
您也可以完全删除 buildEmailBodyString
并在 loadProps
目录中调用它。这将简化代码,因此更容易理解:
function loadProps() {
$("#attachments").html(buildAttachmentsString(item.attachments));
$("#cc").html(buildEmailAddressesString(item.cc));
$("#conversationId").text(item.conversationId);
$("#from").html(buildEmailAddressString(item.from));
$("#internetMessageId").text(item.internetMessageId);
$("#normalizedSubject").text(item.normalizedSubject);
$("#sender").html(buildEmailAddressString(item.sender));
$("#subject").text(item.subject);
$("#to").html(buildEmailAddressesString(item.to));
// Retrieve Email Body
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text, function callback(resText) {
$("#body").text(resText.value);
});
}
关于javascript - 将 body.getAsync() 包装在同步函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764094/