1

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

我编写了一个单独的方法来编码 url,它返回正确的值,但仍然没有为 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(从变量 encodeLink 中设置的值复制),它就可以工作。

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

我需要能够动态设置链接,那么如果不能使用变量,我该怎么做呢?我做错了什么吗?

更新 plannertaskdetails 的 Microsft 文档 https://docs.microsoft.com/en-us/graph/api/plannertaskdetails-update?view=graph-rest-1.0&tabs=javascript

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

4

1 回答 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);
    }
  )
};
于 2021-11-05T07:12:52.950 回答