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
Jay Rhick
  • 53
  • 5

2 Answers2

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

asg86260
  • 451
  • 3
  • 4
  • how can i use populate? im using Postman to insert and fetch data from the server – Jay Rhick Mar 19 '21 at 01:47
  • do you have a GET endpoint for this collection? Or are you just looking at the returned object from the POST? – asg86260 Mar 19 '21 at 01:51
  • 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 – Jay Rhick Mar 19 '21 at 01:54
  • oh I think this might be how you're building your object from your req.body.books. You're nesting the books array in another array. also maybe just a typo in the req.body.book instead of req.body.books which is what you have in the json example. – asg86260 Mar 19 '21 at 02:13
  • 1
    Thank you so much! You gave me idea to solve this problem! Thank youuuu – Jay Rhick Mar 19 '21 at 02:21
  • Nice! what was the solution, just curious? – asg86260 Mar 19 '21 at 02:23
  • 1
    i will post the answer :) – Jay Rhick Mar 19 '21 at 02:25
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

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64
Jay Rhick
  • 53
  • 5