【问题标题】:My SQL database sequelize npm我的 SQL 数据库 sequelize npm
【发布时间】:2018-04-02 18:52:38
【问题描述】:

听说我使用 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(database) 查询。

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);
});

【问题讨论】:

    标签: mysql node.js sequelize.js


    【解决方案1】:

    试试这个:

    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);
    })
    

    ;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 2016-07-08
      • 2021-03-15
      相关资源
      最近更新 更多