Help needed with postman request issue when condition is not met
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
var cookieParser = require('cookie-parser');
var mongoose = require('mongoose');
app.set('view engine', 'pug');
app.set('views', 'views');
app.use(express.json());
app.use(bodyParser.json( { extended: true} ));
app.use(bodyParser.urlencoded({extended: true }));
app.use(upload.array());
app.use(cookieParser());
mongoose.set('strictQuery', true);
mongoose.connect('mongodb://127.0.0.1:27017/my_db',
{
useNewUrlParser: true,
useUnifiedTopology: true
},(err) => {
if(err){
}else{
console.log("connected");
}
}
);
var userSchema = mongoose.Schema({
username: {type: String, required: true, index: { unique: true }},
password: {type: String, required: true}
});
var User = mongoose.model("User", userSchema);
app.post('/signup', (req, res) => {
var userInfo = req.query;
if(!userInfo.username || !userInfo.password ){
res.status("400");
res.send("Invalid Details");
}else {
const newUser = new User({
username: userInfo.username,
password: userInfo.password
});
User.findOne({username: userInfo.username} , (err, response) => {
if(response == null){
newUser.save().then( ()=> {
// Code here to handle successful signup
}).catch( error => {
// Code to handle errors during saving
});
}
// Need help with handling result in front end
});
}
});
app.listen(3000);
Seeking guidance on stopping code execution at "then" or catch block for displaying results on the frontend, experiencing issues with postman requests unless specific condition is met.