Friday, 2 June 2017

My post operation is not working

I am working on a project that involves registering users into a mongodatabase.

I have my Mongoose Schema in a file called user.js, here is the code in it:

const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const passportLocalMongoose = require('passport-local-mongoose');

let Schema = mongoose.Schema;


let UserSchema = Schema({
  name: {
    type: String
  },

  username: {
    type: String,
    unique: true,
    required: true
  },

  profilePic: {
    type: String
  },

  email: {
    type: String,
    unique: true,
    required: true
  },

  password: {
    type: String,
    required: true,
  },

  usersRecipes: [{type: Schema.Types.ObjectId, ref:'Recipe'}],

  userComments: [{type: Schema.Types.ObjectId, ref: 'Recipe'}]

});

let User = mongoose.model('User', UserSchema);

module.exports = User;

And I here is the code for my file userRouter.js where my code is not working

const express = require('express');
const passport = require('passport');
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');


router = express.Router();

router.get('/', (req, res, next) => {
    res.send('Here are the users!')
});

router.get('/:username', (req, res, next) => {
    let nameQuery = {username: req.params.username};

    User.findOne(nameQuery, (err, user) =>{
        if (err) throw err;
        res.json(user);

    })
});

router.post('/register', function(req, res, next, err){

    User.create({
        username: req.body.username,
        password: req.body.password,
        name: req.body.name,
        email: req.body.email,
        profilePic: req.body.profilePic
    }, (err, user) => {
        if (err) return err;

        res.json(user);
    }); 
});

Everytime I try to add a user in postman, I get this error: enter image description here

What am I doing wrong?



via T-Dot1992

No comments:

Post a Comment