在mysql中查询之后,如何将json响应返回到路由?

hjzp0vay  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(223)

我正在使用elasticsearch、node和mysql。我需要从mysql同步一些用户数据到elasticsearch。我的路线设置如下:

router.post("/register_user", (req, res, next) => {
    mysql.register(req.body).then((result) => {
        elastic.createUser(...);
    });
});

当用户投递到此路由时,它会成功地在mysql中创建一行:

const mysql = require("mysql");
const connection = mysql.createConnection("...");
connection.connect();

exports.register = (req, res) => {
    const user = { name: req.name };
    connection.query('INSERT INTO user SET ?', user, (err, rows) => {
        // stuff for errors
        // ...
        connection.end();
        // what do I do here?
    });
});

我试过:

// I got an error regarding "status of undefined"
res.status(200).json({ id: rows.insertId });

// I got something about "then of undefined" in the router
return { id: rows.insertId };

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题