javascript - 将 body.getAsync() 包装在同步函数中

标签 javascript office-js outlook-web-addins

所以我在关注 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/

相关文章:

angular - 如何在 Web 应用程序中预览 EML 文件?

javascript - 手机上的 Outlook 插件激活

javascript - NODE.JS:当表单提交时出现后端错误时,如何让前端 JS 事件监听器?

javascript - 您将如何以最简单和最简单的方式将对象按字母顺序排列 (Javascript)

javascript - 关闭和功能提升 - 不适用于 Firefox

office-js - 有条件地启用/禁用按钮控制?

javascript - jQuery::当我使用 fadeIn() 时,我的页面移动了它的焦点!

javascript - WordJS/OfficeJS如何获取段落的唯一id

outlook-addin - 用户界面对话框打开时无法访问漫游设置

javascript - 如何在 Outlook 加载项中存储凭据