Express应用程序无法从我的数据库(Sqlite)中获得某些项目
我正在创建一个应用程序,用户可以在其中创建帖子并对此发表评论。创建,更新和删除帖子可以按预期工作,创建评论也可以。
当用户创建评论时,其accountId将传递到数据库。
[删除特定注释时,传递accountId以验证是否允许用户删除它。
问题是,尽管查询要求从数据库表中获取名为“ comments”的所有详细信息,但似乎没有从数据库中获取accountId。
该应用程序分为两个文件db.js和app.js。
我已尝试修改请求。为了进行故障排除,我添加了一行代码,检查是否已获取comment.accountId,但这是我收到错误的地方。
/* in db.js: */
//get comment by comment id
exports.getCommentById = (id, callback) => {
const query = 'SELECT * FROM comments WHERE id = ?'
const values = [ id ]
db.all(query, values, (error, comment) => {
if (error) {
console.log(error)
callback(['databaseError'])
return
} else if (!comment) {
console.log(error)
callback(['notFound'])
return
} else {
callback([], comment)
}
})
}
/* in app.js */
app.delete('/comments/:commentId', (req, res, next) => {
const commentId = req.paramsmentId
db.getCommentById(commentId, (errors, comment) => {
if (errors.length > 0) {
res.status(500).json({
message: 'serverError'
}).end()
return
} else if (!comment) {
res.status(404).json({
message: 'notFound'
}).end()
return
}
const accountId = req.accountId //from my auth middleware
const commAccId = comment.accountId
if(!commAccId) {
console.log(accountId)
console.log(commAccId)
res.status(404).json({
message: 'AccIdNotFound'
}).end()
return
}
- - - - - ^ this is the error checking I inserted, and this is where the error is thrown, so it seems like the id is just not found.
if(!accountId) {
res.status(401).json({
message: 'notAuthenticated'
}).end()
return
} else if (comment.accountId != accountId) {
res.status(401).json({
message: 'notAuthorized'
}).end()
return
}
//plus code for deletion (will insert if it seems relevant, just ask)
})
})
错误消息是“ AccIdNotFound”
console.log返回5(与登录用户相同,并且未定义)>>
我正在创建一个应用程序,用户可以在其中创建帖子并对此发表评论。创建,更新和删除帖子可以按预期工作,创建评论也可以。当用户创建一个...
回答如下:db.all提供了一组行数组,而不仅仅是一行。您假设结果仅是一个注释。