Recently, I've been working on developing a blog site for my class project. The main goal is to create a REST API for the blog site. I have successfully managed to display data from the database using .ejs views, except for one issue. I am facing difficulty in pushing the blogID through when it comes to updating a specific blog with a submit button. I've spent the last 6 days tirelessly searching and testing various solutions but haven't made progress. If anyone could take a look at my code and provide assistance, I would greatly appreciate it. Thank you.
<form method="put" action="/blogedit/<%= blogData.blogid %>" style="padding: 20px;">
<div class="form-group">
<label for="blogTitle" class="text-dark">
Blog Title
</label>
<input class="form-control" id="blogTitle" name="blogTitle" value="<%= blogData.blogTitle %>">
<label for="blogText" class="text-dark" style="width: 100%;">
Blog Text
</label>
<input class="form-control" id="blogText" name="blogText" value="<%= blogData.blogText %>"></textarea>
</div>
<input type="submit" class="btn btn-dark" value="Save">
</div>
</form>
This snippet shows the HTML form that I am attempting to submit.
/*GET BLOG EDIT PAGE*/
module.exports.readOne = function (req, res) {
var requestOptions, path;
path = "/api/blog/" + req.params.blogid;
requestOptions = {
url: apiOptions.server + path,
method: "GET",
json: {}
};
request(
requestOptions,
function (err, response, body) {
if (err) {
console.log(err);
} else {
console.log(response.statusCode);
renderBlogEdit(req, res, body);
}
}
);
};
/*Render BLOG EDIT PAGE */
var renderBlogEdit = function (req, res, blogData) {
res.render('blogedit', {
title: 'Edit Blog',
pageHeader: {
title: 'Edit Blog'
},
blogData: blogData,
blogid: blogData._id,
blogTitle: blogData.blogTitle,
blogText: blogData.blogText
});
};
/*Blog Edit Post*/
module.exports.editPost = function (req, res) {
var requestOptions, path, postdata;
path = '/api/blog/' + req.params.blogid;
postdata = {
blogTitle: req.body.blogTitle,
blogText: req.body.blogText
};
requestOptions = {
url: apiOptions.server + path,
method: "PUT",
json: postdata
};
request(
requestOptions,
function (err, response, body) {
if (response.statusCode === 201) {
res.redirect('/bloglist');
} else {
_showError(req, res, response.statusCode);
}
}
);
};
The above content showcases the app_server controller used for editing a blog, while below you'll find the app_api function specifically designed for editing blogs.
module.exports.readOne = function (req, res) {
console.log('Finding blogs', req.params);
if (req.params && req.params.blogid) {
blogSch
.findById(req.params.blogid)
.exec(function (err, blog) {
if (!blog) {
sendJsonResponse(res, 404, {
"message": "blogid not found"
});
return;
} else if (err) {
console.log(err);
sendJsonResponse(res, 404, err);
return;
}
console.log(blog);
sendJsonResponse(res, 200, blog);
});
} else {
console.log('No blogid in request');
sendJsonResponse(res, 404, {
"message": "No blogid in request"
});
}
};
module.exports.editOne = function (req, res) {
console.log("Updating Blog Entry : " + req.params.blogid);
console.log(req.body);
blogSch.findOneAndUpdate(
{ _id: req.params.blogid },
{ $set: { "blogTitle": req.body.blogTitle } },
{ $set: { "blogText": req.body.blogText } },
function (err, response) {
if (err) {
sendJsonResponse(res, 400, err);
} else {
sendJsonResponse(res, 201, response);
}
}
);
};
If anyone can offer any help or guidance, it would be highly appreciated. Whenever I click the submit button on the form, I keep encountering a 404 error indicating the page cannot be found. Thank you very much for your assistance!