I've been encountering an error every time I try to post something.
articlesRouter.post('articles/:target', async (req, res) => {
const target = req.params.target.replaceAll("_", " ")
const article = await Article.findOne({where: {title: target}})
if (article)
{
const author = req.body.commentAuthor
const text = req.body.commentText
await article.createComment({author, text})
console.log("POST METHOD IS WORKING")
}
})
Here's the form:
<form method="post">
<textarea placeholder="Title of your article" id="title" name="articleTitle"></textarea>
<br />
<textarea placeholder="Body of your article" id="body" type="text" name="articleBody"></textarea>
<br />
<button id="submit" type="submit">Done</button>
</form>
Using the GET method:
articlesRouter.get('/articles/:target', async (req, res) => {
const target = req.params.target.replaceAll("_", " ")
const searchResult = await Article.findOne({where: {title: target}})
if (searchResult)
{
const correspondingComments = await searchResult.getComments()
res.render('../views/currentArticle', {title: searchResult.title, text: searchResult.body, comments: correspondingComments})
}
else{
console.log('Error')
}
})
As you can see, the path to the article includes /articles
and /title_of_article
. I suspect the issue may lie with this line
articlesRouter.post('articles/:target'.....
Could it be that I can't use the post method with /:params??? Thanks for taking the time to read!