NodeJS 如何使用o.js为odata添加日期时间过滤器

u3r8eeie  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(117)

我使用了这个npm包:o.js操作oData服务
我正在尝试获取一个实体,但我想添加一个过滤器:仅获取LastChangeDateTime大于过去五分钟的位置
我做错了什么
查询:

static async fetchRecentProjectTasks(odataHandler) {
        var pastFiveMinutes = moment().subtract(5, 'minutes').format();
        return await odataHandler.get('ProjectTaskCollection').query({
            $filter: `LastChangeDateTime ge datetime'${pastFiveMinutes}'`,
            $top: parseInt(20000),
            $select: "ObjectID,LastChangeDateTime",
            $orderby: "ObjectID",
            $format: 'json'
        });
    }

这是请求的输出:
https://xxx.xxx.com/sap/byd/odata/cust/v1/odata_project/ProjectTaskCollection?%24filter=LastChangeDateTime+ge+datetime%272021-01-10T18%3A04%3A35-05%3A00%27&%24top=20000&%24select=ObjectID&%24orderby=ObjectID&%24format=json

错误:

{"error":{"code":"","message":{"lang":"en","value":"Invalid parametertype used at function 'ge'"}}}
wnrlj8wa

wnrlj8wa1#

我通过尝试一种不同的字符串连接方法解决了这个问题,并且在将日期格式化为toISOString()时使用了datetimeoffset

static async fetchRecentProjectTasks(odataHandler) {
        var pastFiveMinutes = moment().subtract(5, 'minutes').toISOString();
        return await odataHandler.get('ProjectTaskCollection').query({
            $top: parseInt(20000),
            $filter: "LastChangeDateTime ge datetimeoffset'" + pastFiveMinutes + "'",
            $select: "ObjectID,ID,Name,EarliestPlannedStartDateTime,EarliestPlannedEndDateTime,LastChangeDateTime,ProjectTaskService",
            $orderby: "ObjectID",
            "$expand": "ProjectTaskService,ProjectTaskService/ServiceProductDescription,ProjectTaskService/ProjectTaskServicePeriodPlan",
            $format: 'json'
        }).catch(err => {
            console.log(err.url);
        });
    }

我不明白第一个查询中的错误,但无论如何,只要它有效

相关问题