Ibanez1408
Ibanez1408

Reputation: 5058

Cannot see the data in mongo db

This is how I insert data to a mongo db:

router.post('/', function (req, res, next) {
    var email = req.body.email;
    var user = new User({
        firsName: 'Juan',
        lastName: 'Dela Cruz',
        password: 'super-secret',
        email: email
    });
    user.save();
    res.redirect('/');
});

This is how I connect:

var mongoose = require('mongoose');

var appRoutes = require('./routes/app');

var app = express();
// mongoose.connect('localhost:27017/node-angular');
mongoose.connect('mongodb://localhost:27017/node-angular');

This is how I create my schema:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');

var schema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    password: {type: String, required: true},
    email: {type: String, required: true, unique: true},
    messages: [{type: Schema.Types.ObjectId, ref: 'Message'}]
});

schema.plugin(mongooseUniqueValidator);

module.exports = mongoose.model('User', schema);

I start the mongodb with this command in the command prompt with administrative privileges:

'C:\WINDOWS\system32>"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"'

then I try to access it using this command:

use node-angular

switched to db node-angular

db.users.find()

but I don't get the data in the database. I don't even know if I have successfully updated my database. can you please show me how to this right? Thank you.

Upvotes: 2

Views: 822

Answers (2)

Alagarasan M
Alagarasan M

Reputation: 907

Everything is fine. You just need to change the collection name's first letter to upper-case.

Instead of,

db.users.find();

Use

db.Users.find();

Hope it helps...

Upvotes: 2

Prasheel
Prasheel

Reputation: 1020

save function has a callback. Try using that and check if there is some error that can be traced. You can do something like this.

user.save(function (err) { 
    if (err) return handleError(err); 
    // saved!
  });

Upvotes: 1

Related Questions