req.body Does not Include Entire Mongoose Model

172
April 19, 2018, at 09:19 AM

I am having an annoying problem where req.body does not return the full mongoose model. I'm very new to this stuff so I apologize if this is a dumb question. Here is my model:

var mongoose = require("mongoose");
var campgroundSchema = new mongoose.Schema({
   name: String,
   image: [String],
   image_id: [String],
   description: String,
   author: {
     id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
     },
     username: String
   },
   comments: [
      {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Comment"
      }
   ]
});
module.exports = mongoose.model("Campground", campgroundSchema);

And here is the result I receive when I console.log(req.body):

{ campground: 
   { name: 'w',
     description: 'w',
     author: { id: 5aaaf25986f338289129f8ea, username: 'frff' } } }

If any more information is required please let me know.

Edit: Here is the route handler:

var imageLength;
var newImage = new Array();
router.post("/", isLoggedIn, upload.array("image"),function(req, res){
    imageLength = req.files.length;
    for(var i = 0; i < imageLength; i++){
        cloudinary.uploader.upload(req.files[i].path, function(result) {
            // add cloudinary url for the image to the campground object under image property
            newImage.push(result.secure_url);
            console.log(newImage[i-1]);
            console.log(req.body);
            req.body.campground.image.push(newImage[i-1]);
            console.log(req.body);
        });
    }
Answer 1

Your sending Post request? The body is only what you are including from the client side when you make the post/put request.

Rent Charter Buses Company
READ ALSO
Express Google Search Console error your sitemap file is an HTML page

Express Google Search Console error your sitemap file is an HTML page

I try send my sitemapxml to Google Search Console but every time get

210
passwordHash.verify() is always returning false

passwordHash.verify() is always returning false

I am using password-hash library to hash passwords

158
Report completion of recursive function in node.js

Report completion of recursive function in node.js

I'm building a web crawler in nodejs and Electron

145
how to show the latest changes in my console.log in node.js

how to show the latest changes in my console.log in node.js

This question might be very primitive but I just started learning node stuff so please bear with meI have many console

176