Fix bug if id is undefined on post route

@itbsStefan this is the fix for the bug during our pair-programming. The
request to the server was returning *all* posts if `id` was undefined
and the post for redirecting was the most recently created.
This commit is contained in:
Robert Schäfer 2019-03-26 20:03:11 +01:00
parent 9545e20a22
commit 40224ab18d
2 changed files with 13 additions and 7 deletions

View File

@ -77,7 +77,7 @@ export default {
error,
app: { apolloProvider }
} = context
const idOrSlug = id
const idOrSlug = id || slug
const variables = { idOrSlug }
const client = apolloProvider.defaultClient
@ -87,13 +87,16 @@ export default {
response = await client.query({ query: queryId, variables })
post = response.data.Post[0]
if (post && post.slug === slug) return // all good
if (post && post.slug !== slug) redirect(`/post/${post.id}/${post.slug}`)
if (post && post.slug !== slug) {
return redirect(`/post/${post.id}/${post.slug}`)
}
response = await client.query({ query: querySlug, variables })
post = response.data.Post[0]
if (post) redirect(`/post/${post.id}/${post.slug}`)
if (post) return redirect(`/post/${post.id}/${post.slug}`)
return error({ statusCode: 404 })
const message = 'This post could not be found'
return error({ statusCode: 404, message })
}
}
</script>

View File

@ -40,13 +40,16 @@ export default {
response = await client.query({ query: queryId, variables })
user = response.data.User[0]
if (user && user.slug === slug) return // all good
if (user && user.slug !== slug) redirect(`/profile/${user.id}/${user.slug}`)
if (user && user.slug !== slug) {
return redirect(`/profile/${user.id}/${user.slug}`)
}
response = await client.query({ query: querySlug, variables })
user = response.data.User[0]
if (user) redirect(`/profile/${user.id}/${user.slug}`)
if (user) return redirect(`/profile/${user.id}/${user.slug}`)
return error({ statusCode: 404 })
const message = 'This user could not be found'
return error({ statusCode: 404, message })
}
}
</script>