0

I'm making a small blog until now everything works great but now that I try to add a second Schema for categories I got this error ObjectParameterError: Parameter "obj" to Document() must be an object, got . This is the part of the code that is been interested:

const blog_create_post = (req, res) => {
    const blog = new Blog(req.body);
    const category = new Category(req.body.category)
    ...
}

The first declaration work, but the second one send me the error Full error:

ObjectParameterError: Parameter "obj" to Document() must be an object, got <text>

This is req.body:

body: {
    category: 'wdawda',
    title: 'awdawd',
    snippet: 'dawdaw',
    body: 'dawdawda'
  }

Mongoose Schema:

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

const categorySchema = new Schema({
    category: {
        type: String,
        required: true
    }
});

const Category = mongoose.model('Category', categorySchema);
module.exports = Category;

I tried this:

var things = req.body.category
const category = new Category(things);

and I don't have any error until the next line. This is the whole function:

const blog_create_post = (req, res) => {
    const blog = new Blog(req.body);
    var things = { category: req.body.category };
    const category = new Category(things);
    if (!Category.exists({ category: things })) {
        console.log('category not exist');
        category.save()
        .then((result) => {
            blog.save()
            .then((result) => {
                res.redirect('/blogs');
            })
            .catch((err) => console.log(err));
        })
        .catch((err) => res.send(err));
    } else {
        console.log('category exist');
        blog.save()
            .then((result) => {
                res.redirect('/blogs');
            })
            .catch((err) => console.log(err));
    }
}

the new error is this:

UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{ category: 'awdawd' }" at path "category" for model "Category"
3
  • I'v got another error: SyntaxError: Unexpected token s in JSON at position 0 at JSON.parse (<anonymous>) Commented Aug 9, 2020 at 10:00
  • same error as before, the only things that change is the position number: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) Commented Aug 9, 2020 at 10:16
  • This is req.body: { category: 'dawda', title: 'dawda', snippet: 'awdawd', body: 'awdawdawd' } Commented Aug 9, 2020 at 10:36

1 Answer 1

1

You can try this:

const category = new Category({ category: req.body.category })

This line in the blog_create_post function:

const blog_create_post = (req, res) => {
    const blog = new Blog(req.body);
    const category = new Category({ category: req.body.category });
    ...
}

I pass a object with "category" attribute instead of "category" value directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.