util.promisify old library call - node.js

I've been scratching my head over on how to promisify this call and return data to another function when it becomes available.
syno.query('/webapi/query.cgi', {
api : 'SYNO.API.Info',
version: 1,
method : 'query',
query : 'ALL'
}, function(err, data) {
if (err) return console.error(err);
console.log(data);
});
Can anybody shed some light here ? I'm new to nodejs
the old library that I'm using is this one https://www.npmjs.com/package/synology

try :
const query = () => new Promise((resolve, reject) => {
syno.query('/webapi/query.cgi', {
api: 'SYNO.API.Info',
version: 1,
method: 'query',
query: 'ALL'
}, function(err, data) {
if (err) reject(err);
resolve(data);
});
})
then you can call :
query().then(data => { //do something
})
.catch(e => { //do other thing
})

Related

React - delete request come with error code 404

I've an issue while i'm trying to delete a driver from mySQL db.
Calling my function and passing mapped id (it's working):
<button id="deleteRent" onClick={DeleteVehicles.bind(vehicle.id)}>Delete</button>
Here is my react code:
const DeleteVehicles = (CarId) => {
Axios.delete(`http://localhost:3001/vehicleDelete/${CarId}`)
.then((response) => {
if (response) {
console.log(response)
alert("Sikeres Törlés")
navigate("/admin");
}
else {
console.log("törlési hiba")
}
})
}
and here is my node express request:
app.delete('/vehicleDelete/:CarId'), async (req, res) => {
db.query("DELETE FROM products WHERE id = ?", req.params.CarId,
(err, result) => {
console.log(err)
console.log(result)
if (result) {
res.send(result);
}
})
}
any idea?
axios should be lowercased:
axios.delete(`http://localhost:3001/vehicleDelete/${CarId}`)
Be careful with the closing parentheses on the express code:
app.delete('/vehicleDelete/:CarId', async (req, res) => {
db.query("DELETE FROM products WHERE id = ?", req.params.CarId, (err, result) => {
if (err) return res.status(500).send('Error')
res.status(200).send(result);
})
})
You should run this:
app.delete('/vehicleDelete/:CarId'), (req, res) => {
// make sure your are getting CarId that exists
// and then you delete it
db.query(`DELETE FROM products WHERE id = ${req.params.CarId}`,
(err, result) => {
console.log(err)
console.log(result)
if (result) {
res.send(result);
}
})
}
Also, you don't need to add async as your not using await for the query. The result gives you an object that might look like this:
{
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 34,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}
Now, when you say you receive the 404 status code, it means that you don't have the route on which the request is made. So, http://localhost:3001/vehicleDelete/${CarId} you need to register the route properly at the server.
You should add the catch blocks with Promises, it is recommended practice.
const DeleteVehicles = (CarId) => {
Axios.delete(`http://localhost:3001/vehicleDelete/${CarId}`)
.then((response) => {
if (response) {
console.log(response)
alert("Sikeres Törlés")
navigate("/admin");
}
else {
console.log("törlési hiba")
}
}).catch(console.log);
}

Nodejs Promise "res.end is not a function" error

I'm refactoring my code to remove a "callback hell" using Promises, but encountered an error that I cannot pass. My code receives list of IDs and processes them making few database calls, that is why I had this "callback hell".
Everything worked fine until Promises. The res is equal 0 when I had to respond back to the client.
function processVMDelete(returnedVMIDs){
return new Promise((resolve, reject) => {
var mariasqlClient = dbConnection();
mariasqlClient.query( sqlUpdateDELETE_STATE_ByVMID, [
'DELETE',
returnedVMIDs
], function(err, rows) {
if (err){
reject(err);
}
console.log('finish update');
// dont' need to return anything here
resolve(0);
});
mariasqlClient.end();
});
}
function getListExpVM(){
return new Promise((resolve, reject) => {
var vmList = [];
var mariasqlClient = dbConnection();
mariasqlClient.query( sqlSearch_ByUSERNAMEAndSTATE, [
requesterUsername,
'ACTIVE'
], function(err, rows) {
if (err){
reject(err);
}
vmList = filterExpiredVMs(rows);
var response = {
status : 200,
success : 'Successfull',
data : vmList,
requester: requesterUsername
};
resolve(response);
});
mariasqlClient.end();
});
}
router.post('/processVMs', function(req, res) {
var returnedVMIDs = JSON.parse(req.body.data);
processVMDelete(returnedVMIDs)
.then(res => {
console.log('done');
// check if there is more available for the user:
getListExpVM()
.then(response => {
console.log('sending back list of VMs');
//===>>> ERROR HERE: res.end is not a function
res.end(JSON.stringify(response));
})
.catch(err => {
console.log('error', err.message);
logger.error("Error getting expired VMs: " + err.message);
//===>>> ERROR HERE: res.send is not a function
res.status(500).send({error: err.message})
});
})
.catch(err => {
console.log('error', err.message);
logger.error("Error processing VMs: " + err.message);
//===>>> ERROR HERE: res.send is not a function
res.status(500).send({error: err.message})
});
});
You've redefined res with this:
processVMDelete(returnedVMIDs)
.then(res => {...})
This will hide the higher scoped res associated with the overall request (the one you need to use for res.end()). Change the name of this one to something else like result and then change the corresponding references that use this result.

How to ensure that one function completes before other function

I am fairly new to Node.js, and what I am trying to achieve is to have two separate functions. One for Auth and one for sending data (So that I don't run into rate login limits if I were to simply use a callback after conn.login finishes). I tried to set this up in node like this:
var _request = {
url: '/services/data/v45.0/actions/custom/flow/Test1',
method: 'POST',
body: JSON.stringify({
"inputs": [{}]
}),
headers: {
"Content-Type": "application/json"
}
};
var conn = new jsforce.Connection({
clientId: process.env.cliendId,
clientSecret: process.env.clientSecret,
version: "45.0"
});
function sfdcAuth() {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
console.log(err)
}
conn = conn;
console.log("Done")
});
}
function sfdcQuery() {
conn.request(_request, function(err, resp) {
console.log(resp);
console.log(err)
});
}
sfdcAuth()
sfdcQuery()
But because js is asynchronous it runs the second function without waiting for the first function to finish.
The simplest way is to pass your second function as a callback to your first function, which it can call when it’s done:
function sfdcAuth(callback) {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
console.log(err);
}
// Invoke callback when done
callback();
});
}
function sfdcQuery() {
conn.request(_request, function(err, resp) {
console.log(resp);
console.log(err);
});
}
// Pass second function as callback to the first
sfdcAuth(sfdcQuery);
You could also make use of promises:
function sfdcAuth(callback) {
return new Promise((resolve, reject) => {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
reject(err);
}
resolve(userInfo);
});
});
}
function sfdcQuery() {
return new Promise((resolve, reject) => {
conn.request(_request, function(err, resp) {
if (err) {
reject(err);
}
resolve(resp);
});
});
}
// Wait for promise to resolve before invoking second function
sfdcAuth()
.then(result => {
// Do something with result
return sfdcQuery();
})
.then(result => {
// You can continue the chain with
// the result from "sfdcQuery" if you want
})
.catch(err => {
// Handle error
});

Saving replaced string data in mongodb using nodejs

I am trying to replace a string in url . Here is image of it
in this image I want to replace lssplalpha with lssplprod which are in pics array. For that I created an api . Here is a code
apiRoutes.get('/SchoolListing_lssplalpha_Replace',function(req, res) { schoolListModel.find({},function(err,check){
if(err){
return console.log(err);
}
else{
for(var i=0;i<check.length;){
var f=0;
for(var j=0;j<check[i].pics.length;j++){
f++;
var newTitle = check[i].pics[j].replace("lssplalpha","lsslprod");
check[i].pics[j] = newTitle;
console.log("after change",check[i].pics[j]);
check[i].save(function (err) {
if(err) {
console.error('ERROR!');
}
});
}
if(j==check[i].pics.length&&j==f){
i++;
}
}
console.log("i value",i);
console.log("check length",check.length);
if(i==check.length){
return res.json({status:true,message:"Updated Schools"}); }
}
});
});
I am getting success response . When I go and check database nothing changed in db. To know the reason I write log of it. When I see logs it was replacing correctly. But I didn't understand why those are not reflecting in database? Here is an image of log in console
Please help me to come out of this
The issue here is you are running a for loop (synchronous) where you are calling the model.save() operation which is asynchronous and the loop keeps iterating but the results of the async calls come later. The process of saving a database item in an array takes some time and Node.js knows this, so it starts the update and then just moves on trying to update the next item in the array. Once the write operation is complete a callback function is run, but by that point the loop has completed and there is no way to know which items finish in what order.
You could use the Bulk Write API to update your models. This allows you to sends multiple write operations to the MongoDB server in one command. This is faster than sending multiple independent operations (like) if you use create()) because with bulkWrite() there is only one round trip to MongoDB.
The following examples show how you can use the bulkWrite.
Using async/await:
apiRoutes.get('/SchoolListing_lssplalpha_Replace', async (req, res) => {
try {
let ops = [];
const docs = await schoolListModel.find({}).lean().exec();
docs.forEach(doc => {
const pics = doc.pics.map(pic => pic.replace("lssplalpha", "lsslprod"));
ops.push({
"updateOne": {
"filter": { "_id": doc._id },
"update": {
"$set": { pics }
}
}
});
});
const result = await schoolListModel.bulkWrite(ops);
console.log('Bulk update complete.', result);
res.status(200).json({
status: true,
message: "Updated Schools"
});
} catch (err) {
res.status(400).send({
status: false,
message: err
});
}
});
Using Promise API:
const bulkUpdate = (Model, query) => (
new Promise((resolve, reject) => {
let ops = [];
Model.find(query).lean().exec((err, docs) => {
if (err) return reject(err);
docs.forEach(doc => {
const pics = doc.pics.map(pic => (
pic.replace("lssplalpha", "lsslprod")
));
ops.push({
"updateOne": {
"filter": { "_id": doc._id },
"update": {
"$set": { pics }
}
}
});
if (ops.length === 500) {
Model.bulkWrite(ops).then((err, result) => {
if (err) return reject(err);
ops = [];
resolve(result);
});
}
});
if (ops.length > 0) {
Model.bulkWrite(ops).then((err, result) => {
if (err) return reject(err);
resolve(result);
});
}
});
})
);
apiRoutes.get('/SchoolListing_lssplalpha_Replace', (req, res) => {
bulkUpdate(schoolListModel, {}).then(result => {
console.log('Bulk update complete.', result);
res.status(200).json({
status: true,
message: "Updated Schools"
});
}).catch(err => {
res.status(400).send({
status: false,
message: err
});
});
});
You are running asynchronous call model.save() in for loop(synchronous)
To make your for loop synchronous you can use for...of loop which works asynchronous, also you will not need to add multiple checks like you have done in your code.
Try following code, it will work
apiRoutes.get('/SchoolListing_lssplalpha_Replace', function (req, res) {
schoolListModel.find({},function (err, check) {
if (err) {
return console.log(err);
}
else {
for (let checkObj of check) {
let newPicArr=[];
for (let pic of checkObj.pics) {
pic = pic.replace("lssplalpha", "lsslprod");
newPicArr.push(pic);
}
checkObj.pics=newPicArr;
checkObj.save(function (err) {
if (err) {
console.error('ERROR!');
}
});
}
return res.json({ status: true, message: "Updated Schools" });
}
});
});

mongoose queries execution order

I'm having a problem in My code using nodejs and mongoose. where I have a function that is supposed to get books from mongodb ( book.findOne) then update them using second query (updateMany).
the problem is that UpdateMany query executes before getting the books ..so my array stays empty and nothing is updated.
I know that node.js is asynchronous but how could i solve this ?
function UpdateBulk(completeValidBooks){
var existingRowsInDb = [];
completeValidBooks.forEach(function(currentBook) {
book.findOne({'ISIN': currentBook.ISIN },
function(error, result) {
existingRowsInDb.push(result);
});
});
book.updateMany(existingRowsInDb, (err,docs) => {
err ? console.log(err) : console.log(`updated ${docs.length}`);
});
}
Using promise you can do something like this:
function UpdateBulk(completeValidBooks) {
GetExistingRows(completeValidBooks).then((existingRowsInDb) => {
book.updateMany(existingRowsInDb, (err, docs) => {
err ? console.log(err) : console.log(`updated ${docs.length}`);
});
})
}
function GetExistingRows(completeValidBooks) {
return new Promise((resolve, reject) => {
var existingRowsInDb = [];
completeValidBooks.forEach((currentBook) => {
book.findOne({
'ISIN': currentBook.ISIN
}, (error, result) => {
existingRowsInDb.push(result);
});
});
resolve(existingRowsInDb);
})
}
Using callback you can do like this:
function UpdateBulk(completeValidBooks) {
GetExistingRows(completeValidBooks, (existingRowsInDb) => {
book.updateMany(existingRowsInDb, (err, docs) => {
err ? console.log(err) : console.log(`updated ${docs.length}`);
});
})
}
function GetExistingRows(completeValidBooks, callback) {
var existingRowsInDb = [];
completeValidBooks.forEach((currentBook) => {
book.findOne({
'ISIN': currentBook.ISIN
}, (error, result) => {
existingRowsInDb.push(result);
});
});
callback(existingRowsInDb);
}

Resources