0

Just trying to learn node/mongo/express, so I'm pretty new. I'm following a book and just trying to get some basics going.

I have the following code in user.js..

var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/auth_demo');

var user = mongoose.Schema({
  username: String,
  password: String
})

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

and then server-auth.js


var User = require('./user')
var express = require('express')
var bcryptjs = require('bcryptjs')

var app = express();
app.use(require('body-parser').json());

app.post('/user', function( req, res, next ) {
  var user = new User({ username: req.body.username });
  bcryptjs.hash(req.body.password, 10, function( err, hash ) {
    user.password = hash
    console.log( err, hash, user )

    user.save( function( err, user ) {
      if( err ) { throw next( err ) }
      res.send( 201 )
    })
  })
})

When I run node server-auth.js and I enter the following command..

curl -X POST -d '{"username": "somename", "password" : "pass"}' -H 'Content-Type: application/json' localhost:3000/user

I can see in console log of node, it shows the object fine..it also says 'created' in response to the command


null
$2a$10$NjymL8LT40ze6DauEDlX.OLzZMHB09uohbWuhiA0JNVLX8oTiGS1q
{ password: '$2a$10$NjymL8LT40ze6DauEDlX.OLzZMHB09uohbWuhiA0JNVLX8oTiGS1q',
  username: 'somename',
  _id: 561537693097776367ef4d87 }

So it all 'looks' ok.

However, if I then connect to mongo, 'show dbs' shows the auth_demo collection, and if I do a db.auth_demo.find() it doesn't show anything (I've tried saving just locally in mongo and thats fine)?

3
  • 1
    did you change database? use auth_demo;. Then call: show collections; Commented Oct 7, 2015 at 15:48
  • Thanks, yes, why is it saving into 'users' though, where was that set ? Commented Oct 7, 2015 at 16:16
  • np, have figured it in comment below, thanks for help. Commented Oct 7, 2015 at 16:35

1 Answer 1

1

Try this once you entered into mongo client:

use auth_demo;
db.users.find();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works, I guess I'm mixing collections/dabases or something? Why is it 'users', where is this set ?
ok, think I can see it from stackoverflow.com/questions/7230953/… how odd, thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.