javascript - 为什么我无法在 Planner 任务参考中将变量作为 URL 发送?
问题描述
我正在尝试通过 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
解决方案
要将变量用作对象键,您需要使用括号语法
例如:
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);
}
)
};
推荐阅读
- data-structures - Angular - 将 FormBuilder 值传递给 Array 会导致空字符串值
- python - 在数据框上应用 GroupWise 'Softmax' 并更新新列
- c - C / bash:在另一个进程死亡时发出信号
- javascript - 你能在 Ionic 页面导航上创建容器转换(共享元素转换)吗?
- php - 如何使用 php 从 foreach 循环中检索 json_encode 数据
- python - 在python中的等号之前打印添加一个额外的加号
- python - 从文件夹中保存的许多其他文件中创建一个唯一文件
- python - 如何根据数据框中的小时数在训练和测试中拆分数据框
- javascript - 推荐滑块 Javascript 中的问题
- php - httpd.conf 文件中没有 mode_deflate