0

ノードとエクスプレス環境に REST バックエンドがあります。私のサービスは次の JSON を生成しています

[
  { "id": 1, "name": "Action" },
  { "id": 2, "name": "Drama" },
  { "id": 3, "name": "Comedy" },
  { "id": 4, "name": "Romance" }
]

ember-data には、次の形式の JSON が必要です

{
 "genres": [
  { "id": 1, "name": "Action" },
  { "id": 2, "name": "Drama" },
  { "id": 3, "name": "Comedy" },
  { "id": 4, "name": "Romance" }
  ]
 }

以下は、JSON を生成しているサポートされたコードです。

all : function(req, res){
    res.header('Access-Control-Allow-Origin', "*");
    if(connection){
        connection.query('SELECT * FROM generes', 
            function(err, rows, fields){
                if(err) throw err;
                res.contentType('application/json');
                res.send(JSON.stringify(rows));
                res.end();
            });
    }
}

JSON 文字列にリソース名を追加するにはどうすればよいですか?

4

1 に答える 1

1

やってみました

res.send(JSON.stringify({'genres': rows})
于 2013-07-26T07:36:42.727 に答える