我的SQL数据库续集npm

时间:2017-10-18 09:27:38

标签: mysql node.js sequelize.js

听说我使用sequelize npm连接mysql数据库

    const Sequelize = require('sequelize');
    var sequelize = new Sequelize('custom_crm', 'root', '', {
        host: "localhost",
        port: 3306,
        dialect: 'mysql'
    });
    sequelize.authenticate().then(() => {
        console.log('Connection has been established successfully.');
    }).catch((err) => {
        console.log('Unable to connect to the database:', err);
    });

    sequelize.query("SELECT id,name FROM `asd123`.`items` WHERE id = 5 LIMIT 1").spread((results, metadata) => {
      console.log("results >>",results);
    });

我希望使用下面的sequelize模式类型转换asd123(数据库)查询。

website_videos = sequelize.define('website_video', {
  id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true },   
  name : { type: Sequelize.CHAR}
});
website_videos.findAll({}).then((data) => {
  console.log('data',data[0]['dataValues']);
}).catch((err) => {
  console.log('Unable to connect to the database:', err);
});

1 个答案:

答案 0 :(得分:0)

试试这个:

website_videos.findAll(
    {
        where: {
            id: user_id
        },
        limit: 1,
        attributes:["id","name"],
    }).then((data) => {
        console.log('data', data[0]['dataValues']);
    }).catch((err) => {
        console.log('Unable to connect to the database:', err);
})