I have a backend api built with express and mongoose that has a post route for creating an apikey. the issue is that when I post with x-www-form-urlencoded with an email in postman everything gets saved correctly, however from the front end with it seems to drop the email field entirely even though I am using body parser with extended url encoding. Anyone have any idea what's happening?Note: for the new user route I have it bound in the server.js alreadyNew User routeconst express = require('express')const saveNewUser = require('../utils/saveNewUser')const bodyParser = require('body-parser')const newUserRoute = express.Router();newUserRoute.use(bodyParser.urlencoded({extended: true}));newUserRoute.post('/', function (req, res) {const email = req.body.emailconst savedUser = saveNewUser(email)res.status(201).send(savedUser)})module.exports = newUserRouteSave new user functionconst uuidv4 = require('uuid/v4')const User = require('../schemas/User')function saveNewUser(userEmail){let uuid = uuidv4()User.create({email: userEmail, apikey: uuid})return ({email: userEmail, apikey: uuid})}module.exports = saveNewUserUser schemaconst mongoose = require('mongoose');const Schema = mongoose.Schema;const UserSchema = new Schema({email: String,apikey: String,});const User = mongoose.model('user', UserSchema);module.exports = UserThe post requestAxios.post("http://Localhost:5000/user", {email: email }, config) .then((response) => console.log(response)) .catch((err) => console.log(err)); };Thank you anyone who looks over this, I will answer any questions necessary, I'm just at a loss as to why the email isnt being saved in mongo.
Submitted January 18, 2020 at 08:17PM by Comfortable-Sky
No comments:
Post a Comment