如何使用node.js创建与mysql的等待连接

nukf8bse  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(395)

我是node.js的新手,我正在尝试创建一个异步 method 使用node.js,因为我需要检查数据库中的一行,然后决定如何处理它。所以我创建了一个名为 sql-service.js ```
const sql = require('mysql');

var connection = sql.createConnection({
host: '0.0.0.0',
user: 'foo',
password: 'fooo'
});

connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}

console.log('connected as id ' + connection.threadId);

});

console.log(connection.state);

module.exports.SignUpUser = (email,password)=>{
connection.query('select * from usuario', function(error, results, fields) {
console.log(results);
});
}

在我的控制器里:

const sqlService = require('../services/sql-service');
exports.post = async(req,res,next)=>{
const Email = req.body.Email;
const Passw = req.body.Password;
console.log(dateT.getdate());

if (fluentValidation.validateEmail(Email) && fluentValidation.isValidLenght(Passw)) {
    try {
        await sqlService.SignUpUser(Email,Passw);
        //emailService.send(req.body.Email,'Nome','Bem vindo ao hanggu');
    } catch (error) {
        console.log(error);
    }

    res.status(201).send({
        Email: "Valid " + req.body.Email,
        Password: Passw.length,
        Send: 's '//Date : dateT.getDateTime()

    });
} else {
    res.status(500).send({
        Error: "Email invalid"
    })
}

}

但我得到的结果是 `undefined` ,我试过了

console.log('The solution is: ', results[0].usuario);

但仍然如此。
wnavrhmk

wnavrhmk1#

您选择什么模式?

var connection = sql.createConnection ({
    host: '0.0.0.0',
    user:'foo',
    password : 'fooo',
    database : 'you_db'
});

测试

相关问题