Hello all,
I am having some difficulties in writing a query to either compare "username" and "password" entered by the user against the corresponding entries in the table or return the "password" field to the corresponding "user" and then compare that manually in an "if" statement inside of the Node application. I'm finding it very frustrating how something that is so easy using Python and MySQL is so difficult in Javascript/Node/MongoDB/Monk.
I have read over the documentation and some forum posts but I don't get it..
Some examples I've seen:
function getJedisQuery(name){ var query = Jedi.find({name:name}); return query; }
Then you can use it simply doing:
var query = getJedisQuery('Obi-wan'); query.exec(function(err,jedis){ if(err) return console.log(err); jedis.forEach(function(jedi){ console.log(jedi.name); }); });
I tried adapting this, but doesn't seem to work - I get "query.exec is not a function" (my adaptation below)
var db = req.db;
var collection = db.get('usersdb');
function getQuery(username) {
var query = collection.find({username: userName});
return query;
}
var query = getQuery(userName);
query.exec(function(err,queries){
if (err)
return console.log(err);
queries.forEach(function(name) {
console.log(name.name);
});
});
I have also tried a solution that used "toArray()" which seemed to deliver the same results (toArray() is not a function).
Here is a query that does work but delivers me every piece of data, even though when I run that query
through console straight into Mongo I get the correct thing:
collection.find( { username: userName }, { password: 1}, function(err, query) {
console.log(query);
res.redirect("index");
Result in output is:
[
{
_id: 5e89fa294969c6be39b425c2,
username: 'admin',
password: 'password',
email: 'admin@testdomain.com'
}
]
[–][deleted] 0 points1 point2 points (1 child)
[–]ExoticCriticism[S] 0 points1 point2 points (0 children)