function is not returning data - node.js

I have the following code
var redis = require("redis"),
client = redis.createClient();
var getuser = function(username) {
var userhash={};
client.hgetall("users."+username, function(err, user) {
userhash=user;
});
return userhash;
};
user_rahul = {
username: 'rahul',
queueno: 1,
sessionId: '6604353811126202'
};
user_namita = {
username: 'namita',
sessionId:'2'
};
client.hmset('users.rahul', user_rahul);
client.hmset('users.namita', user_namita);
var username = "rahul"; // From a POST perhaps
var user1=getuser(username);
console.log(user1);
client.hgetall("users." + username, function(err, user) {
console.log(user);
});
i have created a getuser function to return the value of particular username in userhash but it is not returning please help in finding why it is not returning .
what should i do to get the value return ?

You're mixing sync and async patterns when you make an async call in your sync getUser function. You need to make your getUser function async - e.g:
var redis = require("redis"),
client = redis.createClient();
var getuser = function(username, cb) {
client.hgetall("users."+username, cb);
};
user_rahul = {
username: 'rahul',
queueno: 1,
sessionId: '6604353811126202'
};
user_namita = {
username: 'namita',
sessionId:'2'
};
client.hmset('users.rahul', user_rahul);
client.hmset('users.namita', user_namita);
var username = "rahul"; // From a POST perhaps
getuser(username, function(err, res){
console.log(res);
});
client.hgetall("users." + username, function(err, user) {
console.log(user);
});

Related

await is only valid in async function error - Azure Function

I'm trying to get my login api working inside azure function, but it keeps saying await is only valid in async function. This is an async function so I'm just super confused.
This line
const user = await db.collection('users').findOne({ email: userLoggingIn.email })
is throwing the error.
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
var MongoClient = require('mongodb').MongoClient;
module.exports = async function (context, req) {
MongoClient.connect(process.env.CosmosDBConnectionString, (err, client) => {
let userLoggingIn = ({ email, password } = req.body);
console.log("userLoggingIn");
console.log(userLoggingIn.email);
let send = response(client, context);
if (err) send(500, err.message);
console.log("DBNAME: " + process.env.dbName);
let user;
let db = client.db(process.env.dbName);
const user = await db.collection('users').findOne({ email: userLoggingIn.email })
console.log("USER");
console.log(user);
let userName= user.instagramName;
if (!user) {
send(401, { message: "Auth failed" });
}
const { username } = user;
console.log("PASSWORD");
console.log(context.req.password);
console.log(user.password);
const goodPassword = bcrypt.compareSync(context.req.password, user.password);
if (!goodPassword) {
return send(401, { message: "Auth failed" });
}
const token = jwt.sign(
{
email: user.email,
userId: user._id,
username: userName
},
"secret_this_should_be_longer",
{ expiresIn: "1h" }
);
context.res = { status: 200, token: token, expiresIn: 3600, userId: user._id, username: username};
})
}
function response(client, context) {
return function (status, body) {
context.res = {
status: status,
body: body
};
client.close();
context.done();
};
}
MongoClient.connect(process.env.CosmosDBConnectionString, (err, client) => {
on this line, the annonymous callback function receiving err and client as parameter is the function that needs to be async
MongoClient.connect(process.env.CosmosDBConnectionString, async (err, client) => {

Mongoose create() resolved before async middleware is finished

var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var UserSchema = new mongoose.Schema({
email: {
type: string,
unique: true,
required: true,
trim: true
},
password: {
type: string,
required: true
},
authtokens: {
type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'AuthToken' }]
}
});
//hashing a password before saving it to the database
UserSchema.pre('save', function (next) {
if (this.isNew) {
bcrypt.gensalt(10, function(err, salt) {
if (err) return next(err);
bcrypt.hash(this.password, salt, null, function (err, hash){
if (err) return next(err);
this.password = hash;
console.log('user.password ', this.password);
next();
});
});
} else next();
});
I call this from a controller:
'use strict';
var mongoose = require('mongoose'),
User = mongoose.model('User'),
AuthToken = mongoose.model('AuthToken');
exports.createUser = function(req, res, next) {
if (req.body.email && req.body.password && req.body.passwordConf) {
var userData = {
email: req.body.email,
password: req.body.password,
passwordConf: req.body.passwordConf
};
//use schema.create to insert data into the db
User.create(userData, function (err, user) {
console.log('user created ', user.password);
if (err) {
return next(err);
} else {
return res.redirect('/profile');
}
});
} else {
var err = new Error("Missing parameters");
err.status = 400;
next(err);
}
};
When a createUser is called with email user#email.com, password password, I get the output:
user.password $2a$10$wO.6TPUm5b1j6lvHdCi/JOTeEXHWhYernWU.ZzA3hfYhyWoOeugcq
user created password
Also, looking directly in the database, I see this user with plain text password -> password.
Why is user having plaintext password in the database. How can I store the hash instead?
In short, you forgot you were going into a callback which has a different functional scope and you're still referring to this, which is at that time not actually the "model" instance.
To correct this, take a copy of this before you do anything like launching another function with a callback:
UserSchema.pre('save', function(next) {
var user = this; // keep a copy
if (this.isNew) {
bcrypt.genSalt(10, function(err,salt) {
if (err) next(err);
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) next(err);
user.password = hash;
next();
});
});
}
});
An alternate approach of course is to modernize things and use Promise results with async/await. The bcrypt library which is actually the "core" and not a fork does this right out of the box:
UserSchema.pre('save', async function() {
if (this.isNew) {
let salt = await bcrypt.genSalt(10);
let hash = await bcrypt.hash(this.password, salt);
this.password = hash;
}
});
Aside from the modern approach being generally cleaner code, you also don't need to change the scope of this since we don't "dive in" to another function call. Everything gets changed in the same scope, and of course awaits the async calls before continuing.
Full Example - Callback
const { Schema } = mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
const uri = 'mongodb://localhost/crypto';
var userSchema = new Schema({
email: String,
password: String
});
userSchema.pre('save', function(next) {
var user = this; // keep a copy
if (this.isNew) {
bcrypt.genSalt(10, function(err,salt) {
if (err) next(err);
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) next(err);
user.password = hash;
next();
});
});
}
});
const log = data => console.log(JSON.stringify(data, undefined, 2));
const User = mongoose.model('User', userSchema);
(async function() {
try {
const conn = await mongoose.connect(uri);
await Promise.all(Object.entries(conn.models).map(([k,m]) => m.remove()));
await User.create({ email: 'ted#example.com', password: 'password' });
let result = await User.findOne();
log(result);
} catch(e) {
console.error(e)
} finally {
process.exit()
}
})()
Full Example - Promise async/await
const { Schema } = mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const uri = 'mongodb://localhost/crypto';
var userSchema = new Schema({
email: String,
password: String
});
userSchema.pre('save', async function() {
if (this.isNew) {
let salt = await bcrypt.genSalt(10);
let hash = await bcrypt.hash(this.password, salt);
this.password = hash;
}
});
const log = data => console.log(JSON.stringify(data, undefined, 2));
const User = mongoose.model('User', userSchema);
(async function() {
try {
const conn = await mongoose.connect(uri);
await Promise.all(Object.entries(conn.models).map(([k,m]) => m.remove()));
await User.create({ email: 'ted#example.com', password: 'password' });
let result = await User.findOne();
log(result);
} catch(e) {
console.error(e)
} finally {
process.exit()
}
})()
Both show the password correctly encrypted, since we actually set the value in the model instance:
{
"_id": "5aec65f4853eed12050db4d9",
"email": "ted#example.com",
"password": "$2b$10$qAovc0m0VtmtpLg7CRZmcOXPDNi.2WbPjSFkfxSUqh8Pu5lyN4p7G",
"__v": 0
}

Cannot read property 'collection' of undefined while connecting to mongodb in node.js?

I am trying to login from my signin.ejs file,after click on the login button in
my signin page it shows "Cannot read property collection undefined" what is the
problem.
I defined my route like this in app.js:
app.post('/login', routes.dologin);
I defined my dologin route in index.js:
exports.dologin = function (req, res) {
res.locals.session = req.session;
var user = req.body.user;
db.authenticateUser(user.email, user.password, function ( err, response) {
if (err) {
.......
.......
} else {
.......
........
}
});
};
In my db.js:
var mongo = require('mongoskin'),
crypto = require('crypto');
module.exports = function (config) {
var USERS_COLLECTION = 'users',
ORDERS_COLLECTION = 'orders',
salt = 'supersecretkey',
db;
authenticateUser: function (emailId, password, callback) {
db.collection(USERS_COLLECTION).count({email : emailId, password: encryptPassword(password)}, function (err, count) {
if (err) {
console.log("error authenticating user: " + err);
callback(new Error(err));
} else if (count === 0) {
callback(new Error("emailid/password did not match"));
} else {
callback(null);
}
});
},
}
What is the problem here for getting "Collection undefined"? i think here
everything is right... Is there any problem here?tell me..please Thanks.
You should add this code db = mongo.db('localhost:27017/yourdb');
var mongo = require('mongoskin'),
crypto = require('crypto');
module.exports = function (config) {
var USERS_COLLECTION = 'users',
ORDERS_COLLECTION = 'orders',
salt = 'supersecretkey',
db = mongo.db('localhost:27017/yourdb');
authenticateUser: function (emailId, password, callback) {
db.collection(USERS_COLLECTION).count({
email: emailId,
password: encryptPassword(password)
}, function (err, count) {
if (err) {
console.log("error authenticating user: " + err);
callback(new Error(err));
} else if (count === 0) {
callback(new Error("emailid/password did not match"));
} else {
callback(null);
}
});
},
}

Mongoose how I can return findOne data object

Here the code:
var mongoos = require('./getMongoos.js');
var user = mongoos.addressFromMongoos(username[0]);
console.log(user); // i need the mongoos find here
getMongoos.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userShema = new Schema({
username: String
});
db = mongoose.createConnection('localhost/***');
var user = db.model('User', userShema);
exports.addressFromMongoos = function(username){
user.find({username: username}, function(err, user) {
if (err) {
callback(err, null);
} else {
console.log(user); //works
}
});
};
but i dont konw how to return the find user in the var user:
var user = mongoos.addressFromMongoos(username[0]);
You can't directly return an asynchronous result from a synchronous function. You have to use callbacks instead:
exports.addressFromMongoos = function(username, callback){
user.findOne({username: username}, callback);
};
calling it as:
mongoos.addressFromMongoos(username[0], function(err, user) {
console.log(user);
});

how to get all users in redis

I have the following code .
var redis = require("redis"),
client = redis.createClient();
user_rahul = {
username: 'rahul'
};
user_namita = {
username: 'namita'
};
client.hmset('users.rahul', user_rahul);
client.hmset('users.namita', user_namita);
var username = "rahul"; // From a POST perhaps
client.hgetall("users" , function(err, user) {
console.log(user);
});
I want to get all the users list how i can get all users list this what i tried but its not working.
You are setting the users in their own hash, so when you do hgetall users, you are trying to get all the members of the users hash. You should do:
var redis = require("redis"),
client = redis.createClient();
user_rahul = {
username: 'rahul'
};
user_namita = {
username: 'namita'
};
client.hset('users', user_rahul, 'Another Value, Pass Maybe?');
client.hset('users', user_namita, 'Another Value, Pass Maybe?');
var username = "rahul"; // From a POST perhaps
client.hgetall("users" , function(err, user) {
console.log(user);
});
You should consider using a list instead, if you dont need any data in the second hash value
How about this
var flow = require('flow'); //for async calls
var redis = require("redis").createClient();
function AddUser(user,callback){
flow.exec(
function(){
//AI for Keep unique
redis.incr('nextUserId',this);
},
function(err,userId){
if(err) throw err;
this.userId = userId;
redis.lpush('users',userId,this.MULTI());
redis.hmset('user:'+userId+':profile',user,MULTI());
},
function(results){
results.forEach(function(result){
if(result[0]) throw result[0];
});
callback(this.userId);
}
);
}
user_rahul = {username: 'rahul'};
user_namita = {username: 'namita'};
//Add user
AddUser(user_rahul,function(userId){
console.log('user Rahul Id' + userId);
});
AddUser(user_namita,function(userId){
console.log('user Namita Id' + userId);
});
//users
function Users(callback){
var users = [];
flow.exec(
function(){
redis.lrange('users',0,-1,this);
},
function(err,userIds){
if(err) throw err;
flow.serialForEach(userIds,function(userId){
redis.hgetall('user:'+userId+':profile',this);
},
function(err,val){
if(err) throw err;
users.push(val);
},
function(){
callback(users);
});
}
);
}
//call
Users(function(users){
console.log(users);
});
For Single User
function getUser(userId,callback){
redis.hgetall('user:'+ userId +':profile',function(err,profile){
if(err) throw err;
callback(profile);
});
}
getUser(1,function(profile){
console.log(profile);
});

Resources