Friday, 2 June 2017

Can't manage to store new user with Express & Mongoose

first of all everything so far is localhost. I am trying to create a user backend so with express I am doing my server, mongoDB is my database where I store all of the users in a collection. And mongoose is my tool connect the express and the mongoDB. So I am doing a POST recquest with all of the new user data at the param URL. So this the relevet stuff of my rig: app.js:

var express = require('express');
var path = require('path');
var cors = require('cors');
const bcrypt = require('bcryptjs');
var mongoose = require('mongoose');
var users = require('./routes/users');

var app = express();
app.use(cors());

app.use('/users', users);

In the model folder I created student.js, it is a user.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcryptjs');


const StudentSchema = new Schema({
    first_name: String,
    last_name: String,
    email:{
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    address:
        {   country: String,
            city: String,
            street: String,
            number: Number
        },
    created_at: Date,
    updated_at: Date
});
StudentSchema.pre('save', function(next) {
    var currentDate = new Date();
    this.updated_at = currentDate;
    if (!this.created_at)
        this.created_at = currentDate;

    next();
});
var Student = mongoose.model('Student', StudentSchema);
module.exports = Student;

module.exports.addStudent = function(newStudent, callback){
    bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash(newStudent.password, salt, function(err, hash) {
            if(err) {
                console.log(hash);
                **console.log("There was an erorr" + err);**
            }else {
                newStudent.password = hash;
                newStudent.save(callback);
            }
        });
    });
};

and at the routes folder, users.js:

var express = require('express');
var router = express.Router();
var Student = require('../models/student');
var mongodb = require('mongodb');

router.post('/register', function(req, res, next) {
    var newStudent =new Student( {
        first_name: req.body.first_name,
        last_name: req.body.last_name,
        email: req.body.email,
        password: req.body.password,
        address:
            {
                country: req.body.country,
                city: req.body.city,
                street: req.body.street,
                number: req.body.number
            }
    });

    Student.addStudent(newStudent, function(err,user) {
    if(err){
        res.json({success: false, msg:'Failed to register user'});
    } else {
        res.json({success: true, msg:'User registered'});
    }
    });
});

router.get('/newstudent', function(req, res) {
    res.render('newstudent', { title: 'Add student' });
});

module.exports = router;

So I POSTED thru Postmen the folloing url:

localhost:3000/users/register?first_name=1&last_name=1&email=1&password=123456&country=1&city=1&street=1&number=1

And I am gatting on the console the error:

There was an erorrError: Illegal arguments: undefined, string

I markered the code line brakes with "**"



via Modi Navon

No comments:

Post a Comment