无法读取未定义的属性“ Fname”

时间:2018-09-28 09:40:11

标签: sql-server try-catch httprequest undefined

我无法将新对象发布到数据库中... 每次我在Postman中运行它时,都会返回一个错误:“无法读取未定义的属性“ Fname”。我的代码是这样的: 连接:

const _ = require('lodash');
const express = require('express');
const app = new express();
const sql = require('mssql');
const Joi = require('joi');

var connectionString = {
    user: 'xxx',
    password: 'xxx',
    server: 'xxx',
    database: 'xxx'
};

发布请求如下所示:

app.post('/api/employees', async (req, res) => {

    const employeeSchema = {
        FirstName: Joi.string().max(20).min(2).required(),
        MiddleName: Joi.string().max(50).min(2).required(),
        LastName: Joi.string().max(50).min(2).required(),
        Bday: Joi.number().min(8).required(),
    }
    const error = Joi.validate(req.body, employeeSchema).error;
    if (error) {
        res.status(400).send(error.details[0].message);
        return;
    };

    try {
        const pool = await sql.connect(connectionString);
        const result = await pool.request()

            .input('FirstName', sql.NVarChar, req.body.Fname)
            .input('MiddleName', sql.NVarChar, req.body.Mname)
            .input('LastName', sql.NVarChar, req.body.Lname)
            .input('Bday', sql.Int, req.body.Birthday)
            .query('INSERT INTO EMPLOYEE(Fname, Mname, Lname, Birthday) VALUES(@FirstName, @MiddleName, @LastName, @Bday)');

        res.send(Json.stringify(result.recordset[0]));
    } catch (err) {
        res.status(400).send(`Something went wrong: ${err}`);
    };
});
sql.close();

app.listen(3000, function () { console.log('server is running on port 3000') });

我对http请求等等还很陌生,所以请保持简单。 谢谢

0 个答案:

没有答案