【发布时间】:2020-02-26 10:33:10
【问题描述】:
我正在使用 三层架构 实现一个带有 express 的网络应用程序。我有 blogposts 作为存储在 mysql 数据库中的资源。表格是这样的:
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)
);
现在我想检索一个博文 id sp 我将查询发送到 数据访问层中的数据库 并像这样导出函数:
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)
}
})
}
然后我在我的业务逻辑层使用它:
exports.getBlogpostId = function(callback){
blogRepo.getBlogpostId(function(blogpost, error){
callback(error, blogpost)
})
}
最后我在表示层使用它:
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)
})
})
当我尝试检索 id 时,出现以下错误:
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)
at Connection.format (/web-app/node_modules/mysql/lib/Connection.js:271:20)
at Connection.query (/web-app/node_modules/mysql/lib/Connection.js:189:22)
at Object.exports.getBlogpostId (/web-app/src/dal/blog-repository.js:19:8)
at Object.exports.getBlogpostId (/web-app/src/bll/blog-manager.js:12:14)
at /web-app/src/pl/routers/blogRouter/blogRouter.router.js:39:17
at Layer.handle [as handle_request] (/web-app/node_modules/express/lib/router/layer.js:95:5)
at next (/web-app/node_modules/express/lib/router/route.js:137:13)
我没有在任何地方调用 slice 方法,所以不知道为什么会出现这个错误?
编辑: 发现问题了,忘记在业务逻辑层添加blogId作为参数了!
提前致谢!
【问题讨论】:
-
我在您包含的代码示例中没有看到切片?
-
这就是我不在任何地方使用它的东西...?@evolutionxbox
标签: javascript mysql express slice