javascript - Modify MySQL result on Node.js -


i building api provide files link user. database storing fileid , filename. , file storing in same directory.

+----+---------+ | id |  file   | +----+---------+ | 01 | abc.jpg | | 02 | cde.mp3 | | 03 | efg.doc | +----+---------+ 

when using php build api

 $sql = "select id,file `test’";  if($stmt=$conn->prepare($sql)) {    $stmt->execute();    $stmt->bind_result($id, $file);    $array = array();     while ($stmt->fetch()) {      $array[] = array('id' =>$id, 'path'=>'http:// abc.com/file/' . $file);    }     $json = json_encode($array, json_unescaped_unicode);  } else {  }   print_r($json); 

i can bind result , modify them. result this

[{"id": "01", "path": "http:// abc.com/file/abc.jpg"},  {"id": "02", "path": "http:// abc.com/file/cde.mp3"},  {"id": "03", "path": "http:// abc.com/file/efg.doc"}] 

now , have build again node.js , found tutorial here

the code of outputing on node.js like

 res.json({"error" : false, "message" : "success", "users" : rows} 

any way modify result on nodejs ? seems no bind result function....

the rows object can treated javascript array. you'll need create transformed output:

var users = []; // hold transformed output  (var user in rows) {   // modify file property   user.file = 'some path' + user.file;   // add transformed user 'users' array   users.push(user); }  res.json({"error" : false, "message" : "success", "users" : users}); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -