javascript - 如何在azure移动服务中查询行 "where id IN [some-array-of-numbers]"(服务器脚本)

标签 javascript azure azure-mobile-services

我的任务是通过给定的 ID 列表从我的 clientDevices 表中查询 deviceTokens。然后向该客户端发送推送通知。

我通过将以下数据插入 pushRequests 表来获取 ID 列表:

{
  "alert": "Hello customer!",
  "badge": 1,
  "recipients": [2, 4, 5]
}

我编写了这个服务器端插入函数:

function insert(item, user, request) {
  if (item.recipients) {
    tables.getTable('clientDevices').where(function(ids) {
      return (ids.indexOf(this.id) > -1)
    }, item.recipients).read({
      success: function(results) {
        // . . .
        // Send push notifications to this guys
        // . . .
      }
    })
    item.recipients = JSON.stringify(item.recipients)
  }
  request.execute()
}

但是我收到一个奇怪的错误:

Error in script '/table/pushRequests.insert.js'. Error: The expression 'ids.indexOf(this.id)'' is not supported.

如果不支持indexOf函数,那么如何制作“field IN array”样式过滤器?我可以将数组作为查询参数传递给 mssql.query(sql, params, options) 吗?

PS:我真的不想从给定数组手动构建 where 表达式。

最佳答案

您可以将移动服务 LINQ 样式语法与 in 运算符一起用于 JS,例如:

// find all TodoItem records with id = 2 or 3
var todos = tables.getTable("TodoItem");
todos.where(function(arr) {
    return this.id in arr;
}, [2, 3]).read({
    success: console.log(results);
});

语法是:

table.where(function, parameters).read(options);

该函数类似于 lambda,通过比较当前行 (this) 的属性来返回 true 或 false。一件奇怪的事情是,参数必须指定为函数签名上的参数并单独传入,正如您在上面的 2 和 3 中看到的那样。

关于javascript - 如何在azure移动服务中查询行 "where id IN [some-array-of-numbers]"(服务器脚本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15464832/

相关文章:

javascript - 单击区域加载另一个 jvectormap

c# - 身份验证 token 没有注册级别访问权限

java - 如何使用 azure-cosmos sdk 在 JAVA java 中获取 JSON 数组形式的 cosmos 响应

Azure 移动服务 : Failover and Redundancy

c# - Azure 离线同步。无法添加项目 "Query execution failed with result: ' MISMATCH'。”

javascript - 无法将 appendChild 附加到从另一个框架创建的节点

javascript - 如何在 HTML 的 pre 标签中包装文本?

javascript - 鼠标单击无法提供坐标

asp.net - Azure - 暂存环境的同步数据库

azure - 将 azure 移动服务 autofac 与 nservicebus 注册相结合