0

我是按照winston-mysql文件写的。我已经在 mysql db 中描述了一个用于存储日志的表,但它不起作用。下面是我的 logger.js -

const winston = require('winston');
const winston_mysql = require('winston-mysql')


var options_default = {
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  table    : 'log',
  fields   : { level: 'info', meta: 'metadata', message: 'source', timestamp: 'addDate'}
};
 
var logger = new (winston.createLogger)({
transports: [
  new winston_mysql(options_default)
]
});

module.exports = logger; 

这是我使用winston的控制器文件。创建客户后,它应该在数据库中创建一个日志文件。我没有收到任何错误,但它没有存储到数据库中。我在做什么错误?

const createCustomer = async(req,res,next) => {
  try{ 
       
       var id = Math.floor(Math.random()*9000000) + 10000000;
       var db = req.con;
       const data = {
             "id": id,
            "first_name": req.body.first_name,
            "last_name": req.body.last_name ,
            "username": req.body.username,
            "email": req.body.email,
            "password": req.body.password,
            "mobile": req.body.mobile,
            "address": req.body.address,
            "created_date": CurrentTime,
            
    };
    console.log(data)

    const salt = await bcrypt.genSalt(20);
    data.password = await bcrypt.hash(data.password, salt);
    var pass = data.password;
    console.log(pass)
    
    if (!req.file.filename)
    return res.status(400).send('No files were uploaded.');

    let filename = fs.readdirSync('./src/template-store/temporaryImage/')
    console.log(filename)
    var file = req.files

    let result = db.query(`INSERT INTO customer set ?`,[data],function(err,rows){
   
        if(err){
            res.json({
                success:0,
                message:"An error occurred",
                err
            })
        }
        else{
            logger.log({level:'info',message : 'customer'}) // here I am using logger
            res.json({
                status:1,
                message:"Inserted",
                data: data
            })
            //  mailer(req.body.username,req.body.email,req.body.password)
            
        }
    })
    
}

  catch(error){
       res.json({
           status:0,
           message:"An error occured",
           error:error
       })
  }
}
4

0 回答 0