首页 > 解决方案 > 如何在 uri 中为 Microsoft 图形请求传递日期变量?

问题描述

我正在尝试使用 Microsoft Graph API 取回日历事件列表,但在查询参数中传递日期变量时遇到问题。如果我将 uri 值硬编码为:

uri: 'https://graph.microsoft.com/beta/me/calendar/events?$filter=start/dateTime ge ' + `'2018-12-10T19:30:34.654Z'`,

然后请求有效。但是,如果我尝试使用 toISOString 传递日期变量,则会出现错误。我尝试过使用或不使用反引号,并在 uri 值和 nextMonday 函数中调用 toISOString。还是不行。有人有想法么?谢谢!

function nextMonday(date){
      var monday = new Date(date);
      monday.setDate(monday.getDate() + (1 + 7 - monday.getDay()) % 7);
      return monday.toISOString();
    }

    let date = new Date();
    let m = nextMonday(date);

    const options = {
      uri: 'https://graph.microsoft.com/beta/me/calendar/events?$filter=start/dateTime ge ' + `m`,
      auth: {
        bearer: token,
      },
      headers: {
        'content-type': 'application/json'
      },
      json: true // Automatically parses the JSON string in the response
    };

标签: stringdateurimicrosoft-graph-api

解决方案


您需要 (1) 使用 ${m} 添加值并 (2) 将其用单引号括起来。

乌里:https://graph.microsoft.com/beta/me/calendar/events?$filter=start/dateTime ge '${m}'


推荐阅读