为什么我无法在 Planner 任务引用中将变量作为 URL 发送?

Why am I unable to send a variable as URL in a Planner Task reference?

我正在尝试通过 Planner Graph API 设置任务的引用,但无法通过变量设置 URL。

我已经编写了一个单独的方法来编码 url,它 returns 是正确的值,但仍然没有为 Planner 任务设置引用。

const updateAttachedFiles = (
    links: string[],
    taskId: string,
    etag: string
  ) => {
    var encodedLink: string; 
    links.forEach(async (link) => {
      encodedLink = escapeHtml(link)
      await graph.planner.tasks.getById(taskId).details.update(
        {
          references: {
            encodedLink : {
              "@odata.type": "microsoft.graph.plannerExternalReference",
              "previewPriority": " !",
              type: "Other",
            },
          },
        },
        etag);
    }
    )
  };

 const escapeHtml = (unsafe) => {
    let temp = unsafe.replaceAll("%", "%25")
    unsafe = temp
    .replaceAll(".", "%2E")
    .replaceAll(":", "%3A")
    .replaceAll("@", "%40")
    .replaceAll("#", "%23");

     return unsafe

  }

但是如果我将 encodedLink 更改为硬编码 URL(从变量 encodedLink 中设置的值复制),它就可以工作。

{
          references: {
            "https%3A//shmafe%2Esharepoint%2Ecom/sites/PlannerTest1/Delade dokument/nedladdning%2Ejpg" : {
              "@odata.type": "microsoft.graph.plannerExternalReference",
              "previewPriority": " !",
              type: "Other",
            },
          },
        }

我需要能够动态地设置 link,那么在不能使用变量的情况下我该怎么做呢?我是不是做错了什么?

更新计划任务详细信息的 Microsoft 文档 https://docs.microsoft.com/en-us/graph/api/plannertaskdetails-update?view=graph-rest-1.0&tabs=javascript

plannerExternalReferences 资源类型的 Microsoft 文档 https://docs.microsoft.com/en-us/graph/api/resources/plannerexternalreferences?view=graph-rest-1.0

要将变量用作对象键,您需要使用括号语法

例如:

const myVariable = 'hello';
const demoObject = {
  [myVariable]: 'world'
};

console.log(demoObject[myVariable]);
// same as
console.log(demoObject.hello);

这应该可以解决问题:

const updateAttachedFiles = (
    links: string[],
    taskId: string,
    etag: string
  ) => {
    var encodedLink: string; 
    links.forEach(async (link) => {
      encodedLink = encodeURI(link)
      await graph.planner.tasks.getById(taskId).details.update(
        {
          references: {
            [encodedLink] : {
              "@odata.type": "microsoft.graph.plannerExternalReference",
              "previewPriority": " !",
              type: "Other",
            },
          },
        },
        etag);
    }
  )
};