I’ve gotten the 4th test to pass, but not with validation. I have been researching different ways to validate, and have tried express-validation and mongoose-validation, but it seems like validation is already set up by fcc because I get a default Validation failed Response when I submit the form with a blank name, and the app crashes. I need a good tutorial on how to validate form input using whatever fcc is using. I don’t want to go further without being able to understand what has to happen with validation.
**Your code so far**
const express = require('express')
//const {body, validationResult} = require('express-validator')
//already using mongoose validator
const mongoose = require('mongoose')
//const validate = require('mongoose-validator')
const mongodb = require('mongodb')
const bodyParser = require('body-parser')
const app = express()
const cors = require('cors')
require('dotenv').config();
const db = process.env.DB_URI;
/*
const usernameValidator = [
validate({
validator: 'isLength',
arguments: [1],
message: 'Username should be at least {ARGS[0]} characters long.'
})
]
*/
app.use(cors(), function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(express.urlencoded({extended:false}));
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json())
mongoose.connect(db, {useNewUrlParser:true, useUnifiedTopology:true, useFindAndModify: false});
console.log(db);
const exerciseSchema = new mongoose.Schema({
description: {type: String, required: true},
duration: {type: Number, required: true},
date: {type: String, required: true}
});
const userSchema = new mongoose.Schema({
username: {type:String, required: [true, 'username required']},
log: [exerciseSchema]
});
const Exercise = mongoose.model('Exercise', exerciseSchema);
const User = mongoose.model('User', userSchema);
//create a schema and model for users, then add
//to db and make sure
//response includes _id
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
/*
You can POST to /api/users with form data username to create a new user. The returned response will be an object with username and _id properties.
*/
app.post('/api/users', bodyParser.urlencoded({extended:false}), (req, res) => {
console.log("got this far")
if(Error) {
//res.send('Username is required. Please reload form and try again.'); crashes
return console.error(Error);//would lke to reload form page here
}
let newUser = new User({
username:req.body.username
});
//saves the User to the db and returns the username id properties as json with the response
newUser.save((err, savedUser) => {
if (err) {
return console.error(err);
}
console.log(!savedUser)
console.log("saved");
res.json({
username: savedUser.username,
_id: savedUser._id
})
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
Challenge: Exercise Tracker
Link to the challenge: