1

So I'm trying to insert a nested document here. books contains multiple book but it doesn't insert a title, only auto generated _id.

here's my code inserting a data:

"firstName": "Jay Rhick",
"lastName": "Villareal",
"country": "Philippines",
"citizenship": "Filipino",
"books": [{
    "book": "Harry Pota"
}]

and here's the output:

{
"_id": "6053fc2f33325b35305ee764",
"firstName": "Jay Rhick",
"lastName": "Villareal",
"country": "Philippines",
"citizenship": "Filipino",
"books": [
    {
        "_id": "6053fc2f33325b35305ee765"
    }
],
"__v": 0

Here's my Schema:

const mongoose = require('mongoose')
const BookSchema = require('./BookSchema').schema

const PostSchema = mongoose.Schema({
    firstName: String,
    lastName: String,
    citizenship: String,
    country: String,
    books: [BookSchema]
})

module.exports = mongoose.model('PostSchema', PostSchema)

here's my book schema

const mongoose = require('mongoose')

const BookSchema = mongoose.Schema({
     book: String
})

module.exports = mongoose.model('BookSchema', BookSchema)

and here's my express routes

const express = require('express')
const router = express.Router()
const Post = require('../models/Post')

router.post('/', async (req, res) => {
    const post = new Post({
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        country: req.body.country,
        citizenship: req.body.citizenship,
        books: [{
            book: req.body.book
        }]
    })
    try{
        const savedPost = await post.save()
        res.json(savedPost)
    }catch(err){
        res.json({ message: err })
    }
})

module.exports = router
1
  • If your question seems to be answered, please don't forget to update it, and mark it as solved. Commented Mar 19, 2021 at 6:16

2 Answers 2

1

Since its a nested schema, it might not be returning the whole object, just the _id. Can you see the book with the title in your collection? If it is in the collection, you'll want to take a look at the .populate() function in mongoose.

heres a similar stackoverflow question: Populate nested array in mongoose

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

7 Comments

how can i use populate? im using Postman to insert and fetch data from the server
do you have a GET endpoint for this collection? Or are you just looking at the returned object from the POST?
im trying to insert a book title but it doesn't seems to work, it only shows the _id as it shown above, I also checked the database at atlas mongodb, same output, it doesn't insert a book, only auto generated id
Thank you so much! You gave me idea to solve this problem! Thank youuuu
i will post the answer :)
|
0

I solved my problem thanks to asg86260! So I have changed my code from:

  books: [{
    book: req.body.book
  }]

to books: req.body.books

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.