Unable to use .then in express framework - node.js

I'm new to Express framework and learning, I'm having a problem using .then. The problem is I have 2 functions and I want the first one to complete before the second to start executing. I'm exporting the modules.
var ubm = require('./userBasic');
There are 2 functions setUserData and showUserId, the showUserId must execute only after setUserData has performed its operation.
var userId = ubm.setUserData(userName,userEmail,userDOB,moment);
userId.then(ubm.showUserId(userId));
Below are the 2 functions:
module.exports = {
setUserData: function (userName,userEmail,userDOB,moment){
//Performing database activities
return userId;
}
showUserId: function (userId){
console.log(userId);
}
}
When i run it says TypeError: Cannot read property 'then' of undefined.
Like I said I'm very new and learning and was unable to figure out the solution. I did some google search and got a brief about promise, but I don't know how to implement here.

Try using promises
module.exports = {
setUserData: function(userName, userEmail, userDOB, moment) {
return new Promise(function(resolve, reject) {
//db stuff
reject(error);
resolve(userId);
});
},
showUserId: function(userId) {
console.log(userId);
};
};
So in your execution you would write
ubm.setUserData(username, usereEmail, userDOB, moment)
.then((data) => {
showUserId(data);
})
.catch((err) => {
console.log(err);
});
A couple of things to note is that in this instance you could just log data without the need for another function like
ubm.setUserData(username, usereEmail, userDOB, moment)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(err);
});
Whatever value you pass into resolve() will be returned as well as you pass errors into reject().

Related

Async - Await issue using Twitter API

I'm currently trying to practice using an API with Twitter API.
I'm using Twit package to connect to twitters API but when I try to do a get request I get
Promise { pending }
I have tried using Async-Await but I'm not sure what I'm doing wrong here.
Here is my code:
const Twit = require('twit');
const twitterAPI = require('../secrets');
//Twit uses OAuth to stablish connection with twitter
let T = new Twit({
consumer_key: twitterAPI.apiKey,
consumer_secret: twitterAPI.apiSecretKey,
access_token: twitterAPI.accessToken,
access_token_secret: twitterAPI.accessTokenSecret
})
const getUsersTweets = async (userName) => {
let params = { screen_name: userName, count: 1 }
const userTweets = await T.get('search/tweets', params, await function (err, data, response) {
if (err) {
return 'There was an Error', err.stack
}
return data
})
return userTweets
}
console.log(getUsersTweets('Rainbow6Game'));
Problem
The biggest assumption that is wrong with the sample code is that T.get is expected to eventually resolve with some data.
const userTweets = await T.get('search/tweets', params, await function (err, data, response) {
if (err) {
return 'There was an Error', err.stack
}
return data // 'data' returned from here is not necessarily going to be received in 'userTweets' variable
})
callback function provided as last argument in T.get function call doesn't have to be preceded by an 'await'.
'data' returned from callback function is not necessarily going to be received in 'userTweets' variable. It totally depends on how T.get is implemented and can not be controlled.
Reason
Thing to be noted here is that async / await works well with Promise returning functions which eventually get resolved with expected data, however, that is not guaranteed here
Relying on the result of asynchronous T.get function call will probably not work because it returns a Promise { pending } object immediately and will get resolved with no data. The best case scenario is that everything with your function will work but getUsersTweets function will return 'undefined'.
Solution
The best solution is to make sure that your getUsersTweets function returns a promise which eventually gets resolved with correct data. Following changes are suggested:
const getUsersTweets = (userName) => {
return new Promise ((resolve, reject) => {
let params = { screen_name: userName, count: 1 }
T.get('search/tweets', params, function (err, data, response) {
if (err) {
reject(err);
}
resolve(data);
})
}
}
The above function is now guaranteed to return expected data and can be used in the following way:
const printTweets = async () => {
const tweets = await getUsersTweet(userName);
console.log(tweets);
}
printTweets();
From what I can see on your code, getUserTweets is an async function, so it will eventually return a promise. I'm assuming you will use this value on another function, so you will need to use it inside an async function and use await, otherwise you will always get a promise.
const logTweets = async (username) => {
try {
const userTweets = await getUsersTweets(username);
// Do something with the tweets
console.log(userTweets);
catch (err) {
// catch any error
console.log(err);
}
}
If logging is all you want and you wrapped it inside a function in which you console.log it, you can call that function directly:
logTweets('someUsername');

NodeJS sqlite3 not returning variable (scope problem)

This code returns undefined when it should be data from the Database. It is part of a class which has the variable this.database (an sqlite3 database). What have I done wrong?
getData(chip_id) {
let user_name;
this.database.get("SELECT data FROM chips WHERE id=?",[chip_id],(err,row) => {
user_name = row.data;
});
return user_name;
}
Retrieving data from database is an asynchronous action and you should handle it with something like Promise or async function. Change your function to this.
getData(chip_id) {
return new Promise((resolve, reject) => {
let user_name;
this.database.get("SELECT data FROM chips WHERE id=?", [chip_id], (err, row) => {
if(err) {
reject(err);
}
else {
user_name = row.data;
resolve(user_name);
}
});
});
}
and in the client side:
getData(id)
.then(username => {
// the username is ready to use
})
.catch(error => {
// handle the error
});
Reading these links also helps you a lot to start:
Callbacks Vs Promises and basics of JS
Getting to know asynchronous JavaScript: Callbacks, Promises and Async/Await
Converting callbacks to promises

How to send multiple objects from node backend to .hbs

I'm currently trying to send 2 objects to the front .hbs front end. However I cant seem to work out how to do this because I'm using promises.
Currently, my thinking is i perform the sql query, the country and organisation name is extracted, and then each sent to a geocoding api, returned and then squashed together in the same promises. But i'm not sure how to extract these for the render function.
Node
//route for homepage
app.get('/', (req, res) => {
let sql = "SELECT org_name, country_name from places;
let query = conn.query(sql, (err, results) => {
if (err) throw err;
const geoPromise = param => new Promise((resolve, reject) => {
geo.geocode('mapbox.places', param, function(err, geoData) {
if (err) return reject(err);
if (geoData) {
resolve(geoData.features[0])
} else {
reject('No result found');
}
});
});
const promises = results.map(result =>
Promise.all([
geoPromise(result.country_name),
geoPromise(result.org_name)
]));
Promise.all(promises).then((geoLoc, geoBus) => {
res.render('layouts/layout', {
results: JSON.stringify(geoLoc),
businesses: JSON.stringify(geoBus)
});
});
});
});
Front end call
results1 = {{{results}}}
console.log(results1.length)
business1 = {{{businesses}}}
console.log(business1.length)
Wrap your geo.geocode into a Promise
const geoPromise = param => new Promise((resolve, reject) => {
geo.geocode('mapbox.places', param, function(err, geoData) {
if (err) return reject(err);
if (geoData) {
resolve(geoData.features[0])
} else {
reject('No result found');
}
});
});
Combine both calls to geo.geocode
const promises = results.map(result =>
Promise.all([
geoPromise(result.country_name),
geoPromise(result.org_name)
]));
Call them
Promise.all(promises).then(([geoLoc, geoBus]) => {
res.render('layouts/layout', {
results: JSON.stringify(geoLoc),
businesses: JSON.stringify(geoBus)
});
});
As MadWard's answer mentions, deconstructing the argument of the callback of Promise.all is necessary since everything will be in the first argument. Make sure you check out his post for more details
Something important to recall: you will never have more than one argument in a then() callback.
Now you may ask: in the case of Promise.all(), what is this value?
Well, it is an array with all the values from the promises it awaits, in the order in which they are called.
If you do:
Promise.all([
resolveVariable1, resolveVariable2, resolveVariable3
]).then((values) => {
})
values will be [variable1, variable2, variable3], the three variables that the promises resolve to.
Your case is, however, a bit more complicated. What is gonna be returned at the end is a 2-D array containing every entry. It is an array of length results.length, and each of its element has a length of 2. The first element is the result, and the second one is the business.
Here is your snippet:
Promise.all(promises)
.then((values) => {
let results = values.map(elmt => elmt[0]);
let businesses = values.map(elmt => elmt[1]);
res.render('layouts/layout', {
results: JSON.stringify(results),
businesses: JSON.stringify(businesses)
});
})

Node.JS Mongodb query / create

I have the following Node.js javascript function:
const createOrUpdateProfile = (pProfile) => {
db.update({facebookId: pProfile.facebookId}, pProfile, {upsert:true}, function (err, profile) {
if (err) {
console.log(err);
};
console.log("profile " + profile.occupation);
return profile;
})
};
The function is called with a user profile object which is just an ordinary object with some user information. Nothing special there.
As a matter of fact the function does what i want it to do. It either finds the profile in the MongoDB database and updates it or it inserts it into the database if it's not found.
The problem is that the profile (new or updated is never returned. I guess it has something to do with the asynchronous nature of Node.
I'm already using a call back function to capture the profile but is doesn't seems to work.
what am I missing here?
EDIT:
I changed the function to look like you sugessted:
const createOrUpdateProfile = (pProfile, callback) => {
db.update({facebookId: pProfile.facebookId}, pProfile, {upsert:true}, function (err, profile) {
if (err) {
console.log(err);
};
callback(profile);
})
};
and i call it from a graphQL mutation like this:
createOrUpdateProfile: (_, { profile }) => {
Profile.createOrUpdateProfile(profile, (cbProfile) => {
// do something with the new profile
console.log(cbProfile.occupation);
})
},
but the cbProfile appears to be undefined.
Am I doing this wrong?
I will have to look into promises later. i just want to get this working first.
Kim
You can't return from an asynchronous call inside a synchronous function. Instead, you can use a callback. Like this:
const createOrUpdateProfile = (pProfile, callback) => {
db.update({facebookId: pProfile.facebookId}, pProfile, {upsert:true}, function (err, profile) {
if (err) {
console.log(err);
};
console.log("profile " + profile.occupation);
callback(profile);
})
};
Then you can call it like this:
createOrUpdateProfile(pProfile, (profile) => {
// do something with the new profile
console.log(profile);
})
If you don't want to use callbacks, then Promises are another way of achieving this functionality.

Wrapping my head around Promise and then

Ive read plenty of articles on Promises and I feel i'm still missing something. Here is an example of what i'm trying to wrap my head around.
File: ad.js
// Using node.js activedirectory
class AD
{
constuctor(options){
this.ad = new ActiveDirectory(options);
}
isUserValid(user, password){
return new Promise((resolve, reject) =>{
ad.authenticate(user, password, (err, auth) ->{
if(err){
reject({ code: 500, message: 'Unknow error'});
}
if(auth){
resolve({code: 200, message: 'ok'});
}
});
)
}
}
module.exports = {
AD: AD
}
File: session.js
createSession(user, password){
var self = this;
return new Promise((resolve, reject) =>{
const ad = new AD("Required parameters");
Code: 1
const result = self._isADValid(ad, user, password);
Code: 2
self._isADValidPromise(ad, user, password)
.then(resp_ad =>{
})
.catch(err =>{
});
);
}
_isADValidPromise(ad, user, password) {
return new Promise((resolve, reject) => {
ad.isUserValid(user, password)
.then(resp_ad => {
resolv(resp_ad);
})
.catch(err => {
reject(err);
});
});
}
_isADValid(ad, user, password) {
return ad.isUserValid(user, password)
.then(resp_ad => {
return resp_ad;
})
.catch(err => {
return err;
});
}
What i'm trying to understand is the following:
Shouldnt "Code 1" return a value from _isADValid. Instead its returning "Promise{ pending }". I though that you only need to use then/catch from a Promise?
So the function _isADValid() is calling the AD function isUserValid which is returning from a Promise and _isADValid() is wrapped in a then/catch which just returns the result.
Code 2 using "_isADValidPromise" works without any issues. And I get that its wrapped in a Promise so its doing what I expected.
If anyone can help clarify why Code 1 is not working
You have to remember that Promises don't resolve until (at least) the next tick (details here). You're expecting the Promise to return synchronously, but Promises are by definition asynchronous.
Your example demonstrates this: If you think about it, your call to authenticate is very likely asynchronous - reaching out to some server to authenticate the user. Promises were invented to wrap asynchronous operations such as this one in a way that provides a nice control flow and avoids callback hell. So in short, any time you use a Promise, it will return asynchronously.
In that light, self._isADValid(ad, user, password); returns a Promise, so naturally, result is a Promise. The only way to get the result out of it is to add a .then() and wait for it to resolve, which will always be at least one tick into the future.
Hope that helps.

Resources