Closed Fcarrion001 closed 7 years ago
If you want to find a blog by id, I believe the syntax would be Blog.find({id : req.params.id})
sure no problem these are the routes
'use strict'
module.exports = require('lib/wiring/routes')
// create routes
// what to run for `GET /`
.root('root#root')
// standards RESTful routes
.resources('examples')
// users of the app have special requirements
.post('/sign-up', 'users#signup')
.post('/sign-in', 'users#signin')
.delete('/sign-out/:id', 'users#signout')
.patch('/change-password/:id', 'users#changepw')
.post('/blogs/:id/posts', 'posts#create')
.patch('/blogs/:id/posts', 'posts#update')
.delete('/blogs/:id/posts', 'posts#delete')
.get('/blogs/:id/posts', 'posts#index')
.get('/blogs/:id/posts/:id', 'posts#show')
.resources('users', { only: ['index', 'show'] })
.resources('pages', { except: ['new', 'edit'] })
.resources('blogs', { except: ['new', 'edit'] })
// .resources('posts', { except: ['new', 'edit'] })
this is the curl script
API="http://localhost:4741"
URL_PATH="/blogs"
curl "${API}${URL_PATH}/${ID}/posts/${POST_ID}" \
--include \
--request GET \
--header "Authorization: Token token=$TOKEN"
echo
and this is the actual working hardcoded code the code in the original comment was incorrect
const show = (req, res, next) => {
// const findBlogId = req._parsedUrl.pathname.split('/')
// const blogId = findBlogId[4]
console.log('this is req.params.id ', req.params.id)
return Blog.find()
.then(blogs => {
console.log('this is blogs ', blogs)
console.log('this is blogs[0].posts[0] ', blogs[0].posts[0])
return blogs[0].posts[0]
})
// .then(blogs => {
// console.log('this is blogs that is being put into map ', blogs)
// return blogs
// })
.then(blogs => res.json({
post: [blogs].find((e) =>
e.toJSON({ virtuals: true, user: req.user }))
}))
.catch(next)
}
awesome thank you I will try this and report back
Update The above code returns an empty array because the params for my request equal the id of post I am thinking perhaps I will try to use .find() to check for blog matching blog.id then
.then.(blog => blog.find({id: req.params.id})
.then(
'not' .then.(
I'm wondering why you are including an additional /posts/:id
at the end of your route for showing one blog? Wouldn't .get('/blogs/:id'
work fine?
Edit: I see, you're using a nested schema. Got it
It is a show on a post which is nested in a blog
Is this resolved?
Issue resolved I was putting the wrong id for blog in my curl script Once that was fixed I looped through the post array to find the matching id to show
const show = (req, res, next) => {
const findBlogId = req._parsedUrl.pathname.split('/')
const blogId = findBlogId[2]
console.log('this is req.params.id ', req.params.id)
return Blog.find({_id: blogId})
.then(blog => {
console.log('this is blogs ', blog)
console.log('this is blog[0].posts[0] ', blog[0].posts)
blog[0].posts.forEach(function (post) {
if (post.id === req.params.id) {
console.log('this is post ', post)
res.json({
post: post.toJSON({ virtuals: true, blog: req.blog })
})
}
})
})
As is, I have it hardcoded to show a specified post and it works but I cannot figure out how to set up a query where only blog._id === req.params.id The closest I can get without hardcoding is an array of all blogs
I've tried findOne. findById $where Help please