邮递员x-www-form-urlencoded会为POST请求返回空值,即使输入字段已填写

时间:2018-12-19 00:07:45

标签: node.js postgresql postman

我使用了邮递员的raw选项,并且有效,我的输入值按预期返回。但是,当我使用x-form-urlencoded选项时,将返回“ null”。获取请求都很好。 在邮递员横截面的屏幕截图下方找到:

Postman Snapshot

我使用以下postgres脚本手动创建了表和行:

CREATE TABLE graduates (id SERIAL PRIMARY KEY, name TEXT);

CREATE TABLE offers (id SERIAL PRIMARY KEY, title TEXT, graduate_id INTEGER REFERENCES graduates (id) ON DELETE CASCADE);

INSERT INTO graduates (name) VALUES ('Elie'), ('Michael'), ('Matt'), ('Joel');

INSERT INTO offers (title, graduate_id) VALUES ('Teacher', 1), ('Super Teacher', 2), ('Mathematician', 3), ('Developer', 4), ('Super Doctor 1', 3), ('Super Doctor 2', 4), ('Super Developer 1', 2);

我的毕业生的POST请求路线摘要如下;

// This route is mounted on /graduates in app.js

router.post("/", async (req, res, next) => {
    try {
        const result = await db.query(
            "INSERT INTO graduates (name) VALUES ($1) RETURNING *", 
            [req.body.name]
        );
        return res.status(201).send(result.rows[0]);
    } catch(err) {
        return next(err);
    }
});

我期待一个答案。谢谢。

1 个答案:

答案 0 :(得分:0)

您需要一个解析器才能从请求正文中获取名称。现在有一个未构建的Express。 在顶部添加这两行,它将起作用:

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