In the process of developing a web application using express with a three-tier architecture, I have chosen to use a mysql database to store blogposts as a resource. Here is an illustration of how the table is structured:
CREATE TABLE IF NOT EXISTS blogposts (
blogId INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
content VARCHAR(255) NOT NULL,
posted VARCHAR(255) NOT NULL,
imageFile VARCHAR(255) NOT NULL,
userId INT NOT NULL,
CONSTRAINT blog_id PRIMARY KEY (blogId),
CONSTRAINT id_fk FOREIGN KEY (userId) REFERENCES accounts(personId)
);
My current objective is to retrieve a specific blogpost ID by sending queries to the database through the data access layer. The function for this operation looks like this:
exports.getBlogpostId = function(blogId ,callback){
const query = "SELECT * FROM blogposts WHERE blogId = ?"
const value = [blogId]
db.query(query, value, function(error, blogpost){
if(error){
callback("DatabaseError", null)
}else{
callback(null, blogpost)
}
})
}
After implementing it in the business logic layer:
exports.getBlogpostId = function(callback){
blogRepo.getBlogpostId(function(blogpost, error){
callback(error, blogpost)
})
}
Finally, utilizing it in the presentation layer:
router.get("/:blogId", function(request, response){
const blogId = request.params.blogId
blogManager.getBlogpostId(blogId, function(error, blogpost){
const model = {
error: error,
blogpost: blogpost[0]
}
response.render("blogpost.hbs", model)
})
})
However, upon attempting to retrieve the ID, an error surfaced:
TypeError: val.slice is not a function
at escapeString (/web-app/node_modules/sqlstring/lib/SqlString.js:202:23)
at Object.escape (/web-app/node_modules/sqlstring/lib/SqlString.js:56:21)
at Object.format (/web-app/node_modules/sqlstring/lib/SqlString.js:100:19)
...
Despite not specifically calling the slice method, the error persisted. Why might this occur?
UPDATE: The issue was identified - I had neglected to include blogId as a parameter in the business logic layer!
Thank you for any assistance provided!