2

私の仕事は、与えられたIDのリストによってテーブルdeviceTokensからクエリを実行することです。clientDevicesそして、そのクライアントにプッシュ通知を送信します。

次のデータを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、「フィールドIN配列」スタイルのフィルターを作成するにはどうすればよいですか?配列をmssql.query(sql, params, options)クエリパラメータとしてに渡すことはできますか?

PS:指定された配列からwhere式を手動で作成したくないのですが。

4

1 に答える 1

8

in演算子を使用して、JSのモバイルサービス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);
});

構文は次のとおりです。

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

関数が現在の行(this)のプロパティを比較することによってtrueまたはfalseを返すラムダに似ている場合。奇妙なことの1つは、上記の2と3でわかるように、パラメーターを関数シグネチャーのパラメーターとして指定し、個別に渡す必要があることです。

于 2013-03-18T01:55:18.137 に答える