在另一个异步方法中调用一个异步方法在 office js 中不起作用。在 Web 浏览器中使用功能区中使用的命令脚本
Calling an async method inside another async method does not work in office js. Using the command script used from the Ribbon in a web browser
你知道为什么从我的脚本 commands.ts 中调用的函数不起作用吗?
我正在为 Outlook 创建一个加载项,我正在调用方法“加入”,该方法在单击功能区中的按钮时触发。我遇到的问题是,当我在浏览器中时,永远不会调用方法“replaceAsync”,因此永远不会将通知添加到屏幕上。如果我 运行 Outlook 桌面中的应用程序,通知显示绝对正常(图 1)。这是我正在使用的方法,你能告诉我我所做的是否正确吗?你知道导致浏览器出现此问题的可能原因是什么吗?
非常感谢
Image displaying notification in Outlook Desktop app
function join(event: Office.AddinCommands.Event) {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, result => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log('command failed')
Office.context.mailbox.item.notificationMessages.replaceAsync("video", {
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: `No Matching Link Found`,
icon: "Icon.80x80",
persistent: false
});
} else {
console.log('command success')
}
});
event.completed();
}
replaceAsync 方法将具有给定密钥的通知消息替换为另一条消息。当该方法完成时,将使用 Office.AsyncResult
类型的单个参数调用回调参数中传递的回调函数。但是在您的示例代码中,省略了可选参数(回调)。所以,你永远不知道它什么时候结束。
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/35-notifications/add-getall-remove.yaml
var id = $("#notificationId").val();
Office.context.mailbox.item.notificationMessages.replaceAsync(
id,
{
type: "informationalMessage",
message: "Notification message with id = " + id + " has been replaced with an informational message.",
icon: "icon2",
persistent: false
},
handleResult);
感谢尤金的回复。是的,你是对的,我没有将“handleResult”作为回调选项添加到我的方法中。根据您的建议,我添加了它,但我看不到任何结果。我的 handleResult 选项如下所示:
function handleResult() {
console.log('replaced')
}
但后来我发现了问题所在。我在方法 Office.context.mailbox.item.body.getAsync
之外调用 event.completed() 所以我从来没有告诉主机应用程序处理完成了。我的工作方法现在是这样的:
function join(event: Office.AddinCommands.Event) {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, result => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log('command failed')
Office.context.mailbox.item.notificationMessages.replaceAsync("video", {
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: `No Matching Link Found`,
icon: "Icon.80x80",
persistent: false
});
} else {
console.log('command success')
Office.context.mailbox.item.notificationMessages.replaceAsync("video", {
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: `Matching Link Found`,
icon: "Icon.80x80",
persistent: false
});
}
**event.completed();**
});
}
我希望这对面临同样问题的人有所帮助。
谢谢!
你知道为什么从我的脚本 commands.ts 中调用的函数不起作用吗?
我正在为 Outlook 创建一个加载项,我正在调用方法“加入”,该方法在单击功能区中的按钮时触发。我遇到的问题是,当我在浏览器中时,永远不会调用方法“replaceAsync”,因此永远不会将通知添加到屏幕上。如果我 运行 Outlook 桌面中的应用程序,通知显示绝对正常(图 1)。这是我正在使用的方法,你能告诉我我所做的是否正确吗?你知道导致浏览器出现此问题的可能原因是什么吗?
非常感谢
Image displaying notification in Outlook Desktop app
function join(event: Office.AddinCommands.Event) {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, result => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log('command failed')
Office.context.mailbox.item.notificationMessages.replaceAsync("video", {
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: `No Matching Link Found`,
icon: "Icon.80x80",
persistent: false
});
} else {
console.log('command success')
}
});
event.completed();
}
replaceAsync 方法将具有给定密钥的通知消息替换为另一条消息。当该方法完成时,将使用 Office.AsyncResult
类型的单个参数调用回调参数中传递的回调函数。但是在您的示例代码中,省略了可选参数(回调)。所以,你永远不知道它什么时候结束。
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/35-notifications/add-getall-remove.yaml
var id = $("#notificationId").val();
Office.context.mailbox.item.notificationMessages.replaceAsync(
id,
{
type: "informationalMessage",
message: "Notification message with id = " + id + " has been replaced with an informational message.",
icon: "icon2",
persistent: false
},
handleResult);
感谢尤金的回复。是的,你是对的,我没有将“handleResult”作为回调选项添加到我的方法中。根据您的建议,我添加了它,但我看不到任何结果。我的 handleResult 选项如下所示:
function handleResult() {
console.log('replaced')
}
但后来我发现了问题所在。我在方法 Office.context.mailbox.item.body.getAsync
之外调用 event.completed() 所以我从来没有告诉主机应用程序处理完成了。我的工作方法现在是这样的:
function join(event: Office.AddinCommands.Event) {
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, result => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log('command failed')
Office.context.mailbox.item.notificationMessages.replaceAsync("video", {
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: `No Matching Link Found`,
icon: "Icon.80x80",
persistent: false
});
} else {
console.log('command success')
Office.context.mailbox.item.notificationMessages.replaceAsync("video", {
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: `Matching Link Found`,
icon: "Icon.80x80",
persistent: false
});
}
**event.completed();**
});
}
我希望这对面临同样问题的人有所帮助。 谢谢!