Sunday, 21 July 2019

body-parser [req.body] Is Empty

I've been banging my head against a wall for days about this, and I just can't figure out what the problem is.​I'm pretty new to Node, do I'm sorry if I'm not using the correct terminology or giving the right information.​I'm building an application for practice (a music database) using Mongoose, Express and Node (MEN?). I've built the data models I will need and I'm currently in the process of trying to create a new 'artist' object. I have installed body-parser via NPM and have it 'required' in the app.js file.​I've pasted some of my code below, but if it's easier, here is a link to the GitHub repository.​In my artists.js file, my 'create' route is as follows://CREATE - Create the new artist and add to the DB router.post("/", function(req, res){ //get the data from the form var artistName = req.body.artistName; var artistImage = req.body.artistImage; //create the new artist object var newArtist = {name: artistName, imageUrl: artistImage}; //add to the db Artist.create(newArtist, function(err, newlyCreated){ if(err){ console.log(err); } else { console.log(req.body); console.log("new artist successfully added"); res.redirect("/artists"); } }); }); My Arists schema is as follows:var mongoose = require("mongoose"); //Schema setup var ArtistSchema = new mongoose.Schema({ name: String, imageUrl: String }); module.exports = mongoose.model("Artist", ArtistSchema); And my page with the form containing the data that the object is to be created with is as follows:<% include ../partials/header %>

Add a New Artist

Go back
<% include ../partials/footer %>So, as per my 'create' route, I am logging req.body and it comes out blank - {}An object is being created, but not the artist object I'd expected, but an artists object with no properties, and I can't figure out why.​Any input will be massively appreciated, and please let me know if you need any more info.​EDIT: Formatting

Submitted July 21, 2019 at 05:33PM by Heck_

No comments:

Post a Comment