首页 > 解决方案 > 当我通过邮递员在我的 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");
});
});

标签: sqlnode.js

解决方案


确保使用 bodyParser。

app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json());

推荐阅读