mongo input name saved as a String

Viewed 23

when I use console.log(req.body). It says

{
  'hero[title]': 'Lorem ipsum <br> Dolor sit amet.',
  'hero[description]': 'Dolor sit amet',
   uploadon: 'blahblah'
}

but it should look like:

{
  hero[title]: 'Lorem ipsum <br> Dolor sit amet.',
  hero[description]: 'Dolor sit amet',
  uploadon: 'blahblah'
}

do you know the problem? My HTML looks like:

<div class="field">
    <label for="hero[title]">Titel</label>
    <input name="hero[title]" type="text">
</div>

<div class="field">
    <label for="hero[description]">Beschreibung</label>
    <textarea name="hero[description]" id="" cols="20" rows="5"></textarea>
</div>

UPDATE:

my Mongoose Model is:

var mongoose = require('mongoose');

var heroSchema = new mongoose.Schema({
    title: String,
    description: String,
    image: String
});

module.exports = mongoose.model("Hero", heroSchema);

And this is my Route:

app.post("/dashboard/hero", function(req, res) {
    Hero.create(req.body, function(err, newlyAdded){
        if(err){
            console.log(err);
        } else {
            console.log(newlyAdded);
            res.redirect("/dashboard/hero");
        }
    });
});

I want to embed it into an ejs file.

1 Answers

hero[title] will try to access a variable stored in title and look up a property with it's value in a hero object - which is going to break. That is why it is correctly represented as a string like 'hero[title]' when your request body was parsed to JS object.

Related