0

I have REST backend in node and express environment. My service is generating following JSON

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

Where ember-data requires JSON in following format

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

Following is backed code which is generating 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();
            });
    }
}

How can I add the resource name in JSON string?

4

1 回答 1

1

你有没有尝试过

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