I build a small application with node.js, express.js, passport.js and mongodb. I'm new to this technology so I'm trying to test it locally before updating all the code and connect it with mongolab. Now my problem is that I'm not able to see or query all my user locally with my mongo shell, so, everytime I add a user, I'm not able to see if I really add it or not. So far my code is the following:
app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var port = process.env.PORT || 3000;
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var flash = require('connect-flash');
var session = require('express-session');
var routes = require('./routes/index');
var users = require('./routes/users');
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
database.js (config/database.js)
module.exports = {
url: 'mongodb://localhost/expressauth',
};
user.js (models/user.js)
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var userSchema = mongoose.Schema({
local: {
name: String,
email: String,
password: String,
},
});
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
module.exports = mongoose.model('User', userSchema);
The code is working fine so far, and every time I add a new user I'm able to obtain a user id (something like that 59397add4905d20f0329125c) and a password.
Now I'm trying to see all my users through the mongo shell so I type MONGO in my terminal and then I type SHOW DBS, so I can see all my database.
I can find the database that I create through my database.js (url: 'mongodb://localhost/expressauth') called expressauth, so I switch to it (USE expressauth) and at the very end I try to make a query (something like SELECT * in mysql) with this method that I found in the documentation => db.expressauth.find().
But I don't receive any data. So I'm not able to test my application (which is actually an web app that I found on github: https://github.com/danielgynn/express-authentication/.
Any suggestion why my mongoshell is not working?
via Matto
No comments:
Post a Comment