在针对Azure Cosmos DB非分区集合运行的存储过程中,我在选择查询下运行:
var twentyMinutesBefore = new Date();
twentyMinutesBefore.setMinutes(twentyMinutesBefore.getMinutes() - 20);
var filterQuery = "SELECT TOP 40 * FROM c WHERE (c.transmissionState = 2 AND (" + twentyMinutesBefore + " > c.dateCreated.epoch)) OR c.transmissionState = 0 ORDER BY c.dateCreated.epoch DESC";
我按如下方式执行查询:
var isAccepted = collection.queryDocuments(collectionLink, filterQuery, options, callback);
function callback(err, queryFeed, responseOptions) {
if (err) {
throw err; // <-- Error thrown from this line as per stack trace
}
// Iterate through query feed
}
我收到以下错误:
"Message": "Microsoft.Azure.Documents.DocumentClientException: Message:
{\"Errors\":[\"Encountered exception while executing function. Exception = Error:
{\\"errors\\":[{\\"severity\\":\\"Error\\",\\"location\\":{\\"start\\":63,\\"end\\":66},\\"code\\":\\"SC1001\\",
\\"message\\":\\"Syntax error, incorrect syntax near 'Dec'.\\"}]}
Stack trace: Error: {\\"errors\\":[{\\"severity\\":\\"Error\\",\\"location\\":{\\"start\\":63,\\"end\\":66},\\"code\\":\\"SC1001\\",\\"message\\":\\"Syntax error, incorrect syntax near 'Dec'.\\"}]}
在控制台应用程序中,我尝试解析过滤器查询以验证其格式是否正确。结果如下:
SELECT TOP 40 * FROM c WHERE (c.transmissionState = 2 AND (1512593297244 > c.dateCreated.epoch)) OR c.transmissionState = 0 ORDER BY c.dateCreated.epoch DESC
当我按原样复制此查询并在Cosmos DB查询窗口中运行时,它运行正常并按预期返回结果。
不确定为什么它不会通过存储过程运行。知道'Dec'在错误中意味着什么吗?我的存储过程中没有找到任何这样的字符串。
答案 0 :(得分:1)
使用twentyMinutesBefore.getTime()
,无getTime()
获取UNIX纪元时间,您将获得完整日期,例如&#34; Thu Dec 07 2017 13:26:39 GMT+1100 (AUS Eastern Daylight Time)
&#34;:
var filterQuery = "SELECT TOP 40 * FROM c WHERE (c.transmissionState = 2 AND (" + twentyMinutesBefore.getTime() + " > c.dateCreated.epoch)) OR c.transmissionState = 0 ORDER BY c.dateCreated.epoch DESC";