Tell us what’s happening:
I want to get back a response like this , //JSON res format: [{"_id": data._id, “title”: data.title}] . But it returns the whole collection, I can’t get back a selective item like only the title from the collection.
My code snippet
Your code so far
const bookSchema = new mongoose.Schema({
title: String,
comments: []
})
const Books = mongoose.model("Books", bookSchema);
module.exports = function (app) {
app.route('/api/books')
.get(function (req, res){
//response will be array of book objects
//json res format: [{"_id": bookid, "title": book_title, "commentcount": num_of_comments },...]
Books.find({}, (err, data) => {
res.json(data.title)
})
})
.post(function (req, res){
var title = req.body.title;
//response will contain new book object including atleast _id and title
const newBookTitle = new Books({
title
})
newBookTitle.save(function(err, booksData) {
if(err) return res.json(err)
res.json(booksData)
})
})
data.title returns null value?
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0
.
Challenge: Personal Library
Link to the challenge: