Pierre_T
Pierre_T

Reputation: 1302

Mongoose doesn't save all the fields of my POST request

I have a very simple "social network" app: users can register, write posts, like/unlike them and comment a post.

I have a probleme with my Post Schema :

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

// Create Schema
const PostSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "user",
  },
  text: {
    type: String,
    required: true,
  },
  name: {
    type: String,
  },
  avatar: {
    type: String,
  },
  likes: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: "user",
      },
    },
  ],
  comments: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: "user",
      },
    },
    {
      text: {
        type: String,
        required: true,
      },
    },
    {
      name: {
        type: String,
      },
    },
    {
      avatar: {
        type: String,
      },
    },
    {
      date: {
        type: Date,
        default: Date.now,
      },
    },
  ],
  date: {
    type: Date,
    default: Date.now,
  },
});

module.exports = Profile = mongoose.model("post", PostSchema);

When I receive my POST request for comments...

// @route   POST api/posts/comment/:id
// @desc    Add comment to post
// @access  Private
router.post(
  "/comment/:id",
  passport.authenticate("jwt", { session: false }),
  (req, res) => {
    const { errors, isValid } = validatePostInput(req.body);

    // Check Validation
    if (!isValid) {
      // If any errors, send 400 with errors object
      return res.status(400).json(errors);
    }

    Post.findById(req.params.id)
      .then(post => {
        const newComment = {
          text: req.body.text,
          name: req.body.name,
          avatar: req.body.avatar,
          user: req.user.id,
        };
        console.log("newComment: ", newComment);

        // Add to comments array
        post.comments.unshift(newComment);
        console.log("post: ", post.comments);

        // Save
        post.save().then(post => res.json(post));
      })
      .catch(err => res.status(404).json({ postnotfound: "No post found" }));
  },
);

The only fields that are saved in the post.comments array is the User. Not the other fields (text, name, avatar, date).

My console.log("newComment: ", newComment); properly returns the complete object with all its properties but then, 2 lines below, console.log("post: ", post.comments); only returns the comment _id and the user and those are the only fields saved in the DB...

What did I miss here?

Upvotes: 2

Views: 653

Answers (1)

Suman Kundu
Suman Kundu

Reputation: 1742

There is some Problem in the creation of the schema structure, this is the correct way:

comments: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: "user",
      },
      text: {
        type: String,
        required: true,
      },
      name: {
        type: String,
      },
      avatar: {
        type: String,
      },
      date: {
        type: Date,
        default: Date.now,
      },
    }
  ]

the valid structure is nothing but like this(showing the changes made above):

comments: [{
  user: {},
  text: {},
  // others...
}]

Upvotes: 2

Related Questions