I have a data set in data.json
file. I need to add 'uuid'
field to each record. The project is in Node.js.
I can read the file by using
module.exports.api = function(server, fs) {
// Sample Rest Call
server.get('/api/getData', function(req, res) {
fs.readFile(__dirname + '/data.json', function(err, data) {
if (err) throw err;
res.send(200, JSON.parse(data));
});
});
};
I have the option to use mongoose as well.
This is by no means 100% efficient, there are of course better solutions. But you could manipulate the data returned from the JSON by looping through it and setting the index as the UUID of the current object being looped through. Like this
module.exports.api = function(server, fs) {
// Sample Rest Call
server.get('/api/getData', function(req, res) {
fs.readFile(__dirname + '/data.json', function(err, data) {
if (err) throw err;
let results = JSON.parse(data);
let resultsWithUUID = [];
result.forEach((res, index) => {
res.uuid = index;
resultsWithUUID.concat([res]);
}
res.send(200, resultsWithUUID);
});
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.