Reading and making use of XML saved in DB (LOB) - javascript

connection.execute(
`select SSG_RESP_XML from SSG_SR_RESP_DTLS where SERV_PROV_REQ_ID='141220181657' and SSG_RESP_TYPE='NOTIFICATION'`,
(err, result) => {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
var lob = result.rows[0][0];
console.log(lob);
fs.writeFile('xd.xml',lob, {encoding: 'utf8'}, function (err) {
return doRelease(connection);
});
});
I need to save the xml to a file as well as parse and use it in the further lines in the program.
In xd.xml file [object Object] is seen.
`console.log(lob)` I receive the following
How to fetch the xml from the object?
Thanks in advance.

I would explore two ways.
Use fetchAsString method from oracledb package 1.12.1
Cast your LOB to varchar2
select TO_CHAR(SSG_RESP_XML) from SSG_SR_RESP_DTLS where SERV_PROV_REQ_ID='141220181657' and SSG_RESP_TYPE='NOTIFICATION'

Related

Reading file in Node.js

How to read the data of a file(say, read.txt) without getting the buffered Data?
I mean I want to get the content of the file directly without getting the buffer data?
and hoe to delete the folder in Node.js?
// Try this //
fs = require('fs')
fs.readFile('/read.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
});
try {
fs.rmdirSync(dir, { recursive: true });
console.log(`${dir} is deleted!`);
}catch (err) {
console.error(`Error while deleting ${dir}.`);
}

FS write file is breaking my json, how to correctly update just one key?

I have a simple json object like so.
{
"gymData": {
"previousWorkouts": [
],
"exercises": [
]
}
}
both of the arrays above, are full of objects. I have 2 end points /workouts and /exercises.
my backend is just a simple express server with custom end points. when I add a new workout in the frontend and click submit, the /workouts endpoint behaves correctly.
however, I'm getting some issue when it's not updating the json correctly and resulting in a json error.
this is my node endpoint code
app.post("/exercises", function(req, res) {
fs.readFile('db.json', 'utf8', function (err, data) {
if (err) throw err;
let databaseData = JSON.parse(data);
const workoutExercises = req.body.workoutExercises.workouts
workoutExercises.forEach(exercise => {
const filteredExerciseDatabase = databaseData.gymData.exercises.filter(ex => ex.name === exercise.name)
filteredExerciseDatabase[0].previousWeights.push({date: req.body.date, weight: exercise.weight})
})
const updatedData = JSON.stringify(databaseData)
console.log(updatedData)
fs.writeFile('db.json', updatedData, 'utf8', function(err, data) {
if (err) throw err;
res.status(200).send("Basket was updated");
});
});
})
instead of writing the whole file again. I was wondering if I can just update the specific object key that I need too?
also, for reference, the error seems to be appending extra stuff onto the json object, so it's breaking. but in this line: console.log(updatedData) when I copy that logged data into a json validator, it is valid. so I'm confused as to why it's not writing the correct thing :/

Can't generate PDF from html

tried to use several libraries for pdf generation but I catch always one error:
Fatal Error: spawn UNKNOWN
code, something like is:
mammoth.convertToHtml({
path: './backend/common/template.docx'
})
.then(function (result) {
var html = result.value; // The generated HTML
pdf.create(html).toFile("./backend/common/vvvv.pdf", function (err, res) {
if (err) {
callback(err);
console.log(err);
return;
}
callback(null, res);
});
var messages = result.messages; // Any messages, such as warnings during conversion
console.log(messages);
})
.done();
Help please, what is wrong
I think you miss the import
var pdf = require('html-pdf');
Make sure you have installed the html to pdf converter through node and imported/required the file!

Deleting / removing with Mongoose with multiple parameters

This is my first attempt at deleting data in a MongoDB database. I'm loosely following this tutorial (just the delete part) to no avail, https://www.airpair.com/javascript/complete-expressjs-nodejs-mongodb-crud-skeleton. I just want to delete all the requested people who are in the requested country. All of my other requests work so I will just post the code that I know is not working, everything else is fine.
EDIT
The error I get in the log is "404 Not Found". When testing w/ Postman the response I get is, "Cannot DELETE /deletepeople/USA/John"
app.delete('deletepeople/:country/:name', function(req, res) {
var countryReq = req.params.country;
var nameReq = req.params.name;
peopleModel
.find({"country":countryReq}, function(err, country) {
country.find({"name": nameReq}, function (err, person) {
person.remove(function (err, person) {
if (err) {
console.log(err);
res.status(500).send();
}
return res.status(200).send();
})
})
})
});
});
country.find({"name": nameReq}, function (err, person) {
The above line is causing you an error, what are you searching in a returned document? Its just an document and not a collection.
You can use the id() method in embedded docs:
Look at the subdocuments [http://mongoosejs.com/docs/subdocs.html]

node.js code to append data to a file

How can I append data to a file using node.js
I already have a file named myfile.json with data. I want to check if the file name exists and then append some data to that file.
I'm using following code
var writeTempFile = function (reportPath, data, callback) {
fs.writeFile(reportPath, data, function (err) {
//if (err) //say(err);
callback(err);
});
}
writeTempFile(reportDir + '_' + query.jobid + ".json", data, function (err) {
context.sendResponse(data, 200, {
'Content-Type': 'text/html'
});
You can use jsonfile
var jf = require('jsonfile');
var yourdata;
var file = '/tmp/data.json';
jf.readFile(file, function(err, obj) {
if(!err) {
var finalData = merge(obj, yourdata);
jf.writeFile(file, finalData, function(err) {
console.log(err);
});
}
});
You need to implement your merging logic in merge(object1, object2)
https://npmjs.org/package/jsonfile
Check out the following code.
function addToFile(reportPath, data, callback){
fs.appendFile(reportPath, data, function (err) {
callback(err);
});
}
Node offers fs module to work with file system.
To use this module do
var fs = require('fs')
To append some data to file you can do:
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
Node offers you both synchronous and asynchronous method to append data to file, For more information please refer to this documentation

Categories