How to get values from a inner function in nodejs - javascript

HI HAVE A SIMPLE FUNCTION WHICH TAKE A NUMBER AND VERIFY IT
var verifyNumber =(phoneNumber)=>{
//Number Verification
cb.validatePhone(phoneNumber,'sms',(err,data)=>{
if(err){
console.log(`You got an error `);
}
console,log('Code send');
return data
})
}
verifyNumber('***********');
but the problem is that i want the response which the cb.validatePhone() is given back which is in the 2nd param (data)
and when i return it gave me "undefined" :(
so you can a get the data which is a Object .

There are a number of ways to return data from an async operation.
Here's an example using a callback:
function verifyNumber(phoneNumber, callback) {
try {
cb.validatePhone(phoneNumber, 'sms', (err, data) => {
if (err) throw new Error('You got an error');
callback(data);
});
} catch (e) {
console.log(e);
}
}
verifyNumber('***********', (data) => console.log(data));
Here's one using a promise.
function verifyNumber(phoneNumber) {
return new Promise((resolve, reject) => {
try {
cb.validatePhone(phoneNumber, 'sms', (err, data) => {
if (err) throw new Error('You got an error');
resolve(data);
});
} catch (e) {
console.log(e);
}
});
}
verifyNumber('***********').then(data => console.log(data));

Related

Why re-throwing error in catch block having TypeError

I have a route handler in express.js and inside it, I am calling an asynchronous function which should return me some value. I am using bluebird promises to handle the promises. Below is a sample code.
router.js
---------
router.post('/endpoint', (req, res) => {
return Promise.try(() => serviceFile.validate())
.then(() => {
console.log('Response: ', response);
})
.catch((error) => {
console.log('Error: ', error) // TypeError: expecting a function but got [object Promise]
})
})
serviceFile.js
--------------
async function validate() {
return Promise.try(() => axios.post('/endpoint'))
.then((response) => response.data)
.catch((error) => {
console.log('Error: ', error.response.data) // successfully printing the error data object
throw error;
})
}
When I call the validate() in the Service.js file, it fails the request (which I want) and successfully prints the error. But I don't want to handle the error here, so I re-throw it and expects to handle it in the router.js file. But in the router.js file, I am getting error as undefined and it says, TypeError: expecting a function but got [object Promise]
I am not getting any clue where I am doing wrong. Any help would be greatly appreciated.
its sort of unclear what serviceFile.validate does unless thats the second function you listed, this might be a more clear way of using promises
router.post('/endpoint', async (req, res) => {
try {
const response = await serviceFile.validate()
console.log('Response: ', response);
} catch (err) {
console.log('Error: ', error)
}
})
function validate () {
return new Promise(async (resolve, reject) => {
try {
const res = await axios.post('/endpoint')
resolve(res.data)
} catch (err) {
if (err.response && err.response.data) {
reject(err.response.data)
} else {
reject(err)
}
}
})
}

How to handle errors with Async and Await [Node.js]

I have already a function written with bluebird promises and I would like to rewrite it with async and await. When I have made the changes I have found out that earlier with promises the reject statement always transfers the control to called function catch block though if the catch block is already there in the file from where we are rejecting. How to handle this situation properly with async and await?. (Added comments to the code to explain the issue)
With Promise:
const callingFunc = (req, res) => {
return new Promise((resolve, reject) => {
// execute request which returns promise
functionCall()
.then((response) => {
let error;
try {
xml2js(response.body, { explicitArray: false }, (err, result) => {
if (err) {
return reject(err); /* throws the correct error to catch block of the file from where callingFunc is called*/
}
if (!_.isEmpty(result.Response.errorCode)) {
return reject(result.Response); /* throws the correct error to the catch block of the file from where callingFunc is called*/
}
return resolve(result);
});
} catch (e) {
error = new Error('xml2js conversion error');
reject(error);
}
})
.catch((error) => {
const Error = new Error('Internal Server Error');
reject(Error);
});
});
};
With async and await:
const callingFunc = (req, res) => {
try {
const response = await functionCall();
let error;
try {
xml2js(response.body, { explicitArray: false }, (err, result) => {
if (err) {
throw (err); /* throws the error to the below catch block and returning xml2js conversion error and changing behaviour*/
}
if (!_.isEmpty(result.Response.errorCode)) {
throw result.Response; /* throws the error to the below catch block and returning xml2js conversion error and changing behaviour*/
}
return result;
});
} catch (e) {
error = new Error('xml2js conversion error');
throw error;
}
} catch(error) {
const Error = new Error('Internal Server Error');
throw Error;
}
};
If functionCall returns a promise, then this code is inappropriate...
return new Promise((resolve, reject) => {
// execute request which returns promise
functionCall()
.then((response) => {
If xml2js is async using callbacks, then it is appropriate to wrap it in a promise...
// return a promise that resolves with the result of xml2js
async function xml2js_promise(body) {
return new Promise((resolve, reject) => {
xml2js(body, { explicitArray: false }, (err, result) => {
if (err) reject(err);
else if (!_.isEmpty(result.Response.errorCode)) reject(result.Response);
else resolve(result);
});
});
}
Now we can await these. There's no need to nest the try's. (And you only need the try if you're going to do something on the catch).
async callingFunction = (req, res) => {
try {
const response = await functionCall();
} catch (error) {
// do something with this error
}
try {
const result = await xml2js_promise(response.body)
} catch(error) {
// do something with this error
}
return result;
}

Typescript for a promise that resolves an array of objects

I'd like to generally declare that a function returns a promise that resolves an array of objects (I think that this would resolve a TS error; TS doesn't know that I can use forEach on the resolved array).
On the function openJSON() (see below) I tried things like:
function OpenJSON(): Promise<Array<{id: string, performance: number, average: number}>> { // ...
but I still get errors. And in the future, I expect all sorts of different objects to be produced by this function. Is there just a general way to declare an array of objects, say like Promise<Array<{}>>?
async function produceDataRows() {
let tableContent = '';
let jsonData = await OpenJSON();
// error occurs here: [ts] Property 'forEach' does not exist on type '{}'.
jsonData.forEach(element => {
// convert this JSON data into table content
});
return tableContent;
};
function OpenJSON() {
return new Promise( (resolve, reject) => {
fs.open(kJsonPath, 'wx', (err, fd) => {
if (err) {
if (err.code === 'EEXIST') {
fs.readFile(kJsonPath, 'utf8', function (err, content) {
if (err) throw err;
resolve(JSON.parse(content));
})
} else {
reject( new Error('When openening performance.JSON, in generateHTML.js, an unexpected error has occured. ' + err) );
}
} else throw Error('There is a problem opening performance.JSON');
})
})
}
In this case you need to specify the result type when you construct the promise itself.
function OpenJSON() {
return new Promise<Array<{ id: string, performance: number, average: number }>>(
(resolve, reject) => {
fs.open(kJsonPath, 'wx', (err, fd) => {
if (err) {
if (err.code === 'EEXIST') {
fs.readFile(kJsonPath, 'utf8', (err, content) => {
if (err) {
reject(err);
}
resolve(JSON.parse(content));
})
} else {
reject(Error('When openening performance.JSON'));
}
} else {
reject(Error('There is a problem opening performance.JSON'));
}
});
});
}
Note in the above, I fixed several error handling issues.
Playground link

mongoose Chaining with then and catch

How To Convert This Function to Chaining with then and catch?
Is better to Chained?
I mean User.findOne().then().catch()
User.findOne({_id: msg.chat.id}, (err, doc) => {
if (err) {
console.log(err);
}
if (doc) {
console.log(doc.name);
} else {
console.log('Empty');
}
});
The function you pass to then is called with the returned document (or null) if the operation succeeds, and the catch is called with the error if the operation fails (e.g. no connection). Putting it together looks like this:
User.findOne({_id: msg.chat.id})
.then(doc => {
if (doc) {
console.log(doc.name);
} else {
console.log('Empty');
}
}).catch(err => {
if (err) {
console.log(err);
}
});
As an aside, when you are searching for one document by id, then you can use findById:
User.findById(msg.chat.id)
.then(doc => {
if (doc) {
console.log(doc.name);
} else {
console.log('Empty');
}
}).catch(err => {
if (err) {
console.log(err);
}
});
Better switch to ES2017 async/await syntax, you can avoid Promise Hell
async function foo () {
try {
var doc = await User.findOne({_id: msg.chat.id}).exec()
if (doc)
return console.log(doc.name);
console.log('Empty');
} catch (err) { console.log(err) }
}
foo()
This will help you when you're going to nest DB calls or using for...loops.
async function foo () {
try {
var users = await User.find({}).exec()
for (var user in users) {
var tweets = await Tweet.find({_user: user._id}).exec()
user.tweets = tweets.map(t => t.text)
await user.save()
}
} catch (err) { console.log(err) }
}
foo()

mongodb nodejs store value outside connect

I need a way to store value outside mongoDB connect call:
read(object) {
let result
MongoClient.connect(this.url, function (err, db) {
if (err!=null){
result = err;
} else {
db.collection(object.collection).find(object.field).toArray(function(err, docs) {
assert.equal(err, null);
db.close();
result = docs;
});
}
});
return result
}
When i call this method, which is part of a class, return is called before result assignment, as normal.
Example: console.log(read(obj)) returns undefined
The idea is to store value in a variable and return might wait until connect terminate.
Is there any way to resolve this problem?
Without promise:
Call return inside find and err function:
read(object) {
let result
MongoClient.connect(this.url, function (err, db) {
if (err!=null){
result = err;
return result; //here
} else {
db.collection(object.collection).find(object.field).toArray(function(err, docs) {
assert.equal(err, null);
db.close();
result = docs;
return result; // here
});
}
});
}
Or set a timeout for return with enough time to wait for other process to end:
read(object) {
let result
MongoClient.connect(this.url, function (err, db) {
if (err!=null){
result = err;
} else {
db.collection(object.collection).find(object.field).toArray(function(err, docs) {
assert.equal(err, null);
db.close();
result = docs;
});
}
});
setTimeout(function(){ return result}, 3000); // 3secs
}
With Promise you can try the following:
function read(object) {
let result
return new Promise((resolve, reject) => {
MongoClient.connect(this.url, function (err, db) {
if (err!=null){
reject(err);
} else {
db.collection(object.collection).find(object.field).toArray(function(err, docs) {
db.close();
if (err) {
reject(err);
} else {
resolve(docs);
}
});
}
});
});
}
// then you can call the function and use the result like this
read(obj).then(docs => {
console.log(docs);
})
.catch(err => {
// handle error
console.log(err);
})

Categories

Resources