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

Gio*_*aki 2 javascript azure azure-mobile-services

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

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

{
  "alert": "Hello customer!",
  "badge": 1,
  "recipients": [2, 4, 5]
}
Run Code Online (Sandbox Code Playgroud)

我写了这个服务器端插入功能:

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()
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个奇怪的错误:

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

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

PS:我真的不想手动构建给定数组中的expresion.

Con*_*oob 8

您可以使用in运算符将JS的Mobile Services LINQ样式语法用于示例:

// 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);
});
Run Code Online (Sandbox Code Playgroud)

语法是:

table.where(function, parameters).read(options);
Run Code Online (Sandbox Code Playgroud)

其中函数类似于通过比较当前行(this)的属性返回true或false的lambda.一个奇怪的事情是参数必须在函数签名上指定为参数并单独传递,如上面的2和3所示.