【发布时间】:2020-09-13 17:01:28
【问题描述】:
我正在尝试使用模板文字从我的节点 js 文件中的 mysql 调用一个字段,但无法获取该值。请查看下面的 post.controller.js 文件,其中显示消息:Post ${body.post_id} was successfully created 其中 post_id 是我的 mysql 数据库中的一个字段。
//以下代码在post.service.js文件中
const pool = require("../../config/database");
module.exports = {
//Create new post
createPost: (data, callBack) =>{
pool.query(
`insert into posts(userhandle, post_body)
values(?,?)`,
[
data.userhandle,
data.post_body
],
(error, results, fields) =>{
if(error){
return callBack(error);
}
return callBack(null, results);
}
);
}
}
//以下代码在post.controller.js文件中
const {
createPost,
} = require("./post.service");
module.exports = {
//Controller for creating new post
createPost: (req, res) =>{
const body = req.body;
createPost(body, (err, results) => {
if(err){
console.log(err);
return res.status(500).json({
success:0,
message:"Error. Unable to create post"
});
}
return res.status(200).json({
success: 1,
message: `Post ${body.post_id} was successfully created`,
data: results
});
});
}
}
【问题讨论】:
-
我无法识别服务中查询中使用的此模板语法。你能解释一下吗?
-
那么消息在哪里说:
Post ${body.post_id} was successfully created我正在尝试在成功创建后输入帖子 ID。我希望从 mysql 中提取 post_id
标签: javascript mysql node.js express