尝试根据 Edm.DateTime 类型的 Timestamp 从 Azure 表获取实体时出现错误:
StorageError: A binary operator with incompatible types was detected. Found operand types 'Edm.DateTime' and 'Edm.String' for operator kind 'GreaterThanOrEqual'
let res: IQueryPrePostAuthResponse[] = [];
let query = new TableQuery();
query = query.where('PartitionKey eq ?', searchQuery.user_id);
const entGen = TableUtilities.entityGenerator;
const dateFrom = entGen.DateTime(new Date(searchQuery.date_from));
const dateTo = entGen.DateTime(new Date(searchQuery.date_to));
query = query.and(
'Timestamp >= ? and Timestamp <= ?',
dateFrom,
dateTo,
);
如代码中所示,两个变量(dateFrom和dateTo)都是DateTime类型,而Timestamp是DateTime类型。 根据 Odata Docs link 查询比较运算符,比较运算符两侧的数据类型必须兼容。这是我的情况,两者都是相同的,然后我收到错误。
按日期字段过滤时,请勿将日期值放在引号中。
例如。这是错误的:
http://localhost:7048/XX/ODataV4/PostedSalesInvoice?Posting_Date&%24filter=Posting_Date+ge+'2023-01-01'&%24format=json
这是正确的:
http://localhost:7048/XX/ODataV4/PostedSalesInvoice?Posting_Date&%24filter=Posting_Date+ge+2023-01-01&%24format=json
另外,我有一个有趣的 JavaScript 库,用于构建
odata
URL,以防您感兴趣:
当我在发送到查询之前打印 dateTo 和 dateFrom 时。
const entGen = TableUtilities.entityGenerator;
const dateFrom = entGen.DateTime(new Date(searchQuery.date_from));
const dateTo = entGen.DateTime(new Date(searchQuery.date_to));
console.log(dateFrom);
console.log(dateTo);
我在输出中得到了这个:-
经过大量调试后,我发现我发送 dateFrom 和 dateTo 的方式不正确。
正确的方法是 dateFrom._ 和 dateTo._。但这应该在 Azure 表或 OData 查询中提到,但那里没有。
根据Odata文档链接查询比较运算符,比较运算符两边的数据类型必须兼容。
即使两者的类型相同,也会产生错误,从而造成混乱。正确的代码如下:-
let res: IQueryPrePostAuthResponse[] = [];
let query = new TableQuery();
query = query.where('PartitionKey eq ?', searchQuery.user_id);
const entGen = TableUtilities.entityGenerator;
const dateFrom = entGen.DateTime(new Date(searchQuery.date_from));
const dateTo = entGen.DateTime(new Date(searchQuery.date_to));
query = query.and(
'Timestamp >= ? and Timestamp <= ?',
dateFrom._,
dateTo._,
);