sql - 当我通过邮递员在我的 SQL 查询中传递数据时,它为所有三个字段返回“未定义”
问题描述
module.exports.restinfo = function (restdetail, callback) {
sql.connect(db_config, function (err) {
if (err) {
console.log(err);
sql.close();
callback(err, null);
} else {
var request = new sql.Request();
var createrest = "'INSERT INTO dbo.tbl_childdetails(Division,FirstName,LastName)VALUES" + "(" + restdetail.division + "," + restdetail.firstname + "," + restdetail.lastname + ")";
console.log(createrest);
request.query(createrest, function (err, recordset) {
if (err) {
callback('Error', null);
} else {
callback(null, 'Success');
}
});
}
});
};
返回的结果是:
INSERT INTO dbo.tbl_childdetails (Division, FirstName, LastName)
VALUES (undefined, undefined, undefined)
ErrorErrorResult : null
router.post('/childrest', function (req, res) {
var restdetail = {
"division": req.body.section,
"firstname": req.body.fn,
"lastname": req.body.ln
};
dbops.restinfo(restdetail, function (err, result) {
console.log("Error" + err + "Result:" + result);
res.send("Done");
});
});
解决方案
确保使用 bodyParser。
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
推荐阅读
- c# - DataGrid ItemsSource绑定不刷新单元格值更改/用户编辑
- r - 为什么 R 过滤不适用于特定值?
- flutter - Flutter:我没有从应用内购买中获得翻译产品
- html - 覆盖先例 css 的媒体查询
- c - 与随机 3D 框的射线相交长度
- ios - Xcodebuild 命令失败
- asp.net-core - 我在注册时设置重定向 url 以响应来自 identityserver4 的页面的问题
- git - GIT 变基问题
- python - 如何将字典一一追加到json文件中?
- reactjs - 在 Redux 中,为什么要检查所有 reducer 是否要执行一个案例?