Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize.query() is not a FUNCTION

I am new to sequelize. I am trying to use row query, but I'm getting an error.

Code:

const sequelize = require('sequelize');

sequelize.query("SELECT * from users").then(results => {
      console.log(results);
   })

Error while calling this API:

(node:2380) UnhandledPromiseRejectionWarning: TypeError: sequelize.query is not a function
    at Promise (C:\NodejsProject\mars\app\schedule\models\schedule.js:247:17)
    at new Promise (<anonymous>)

How can I fix this?

like image 460
Nitesh Kanojia Avatar asked Aug 22 '18 09:08

Nitesh Kanojia


People also ask

How do I run a query in Sequelize?

Sequelize instance comes with the query() method which you can use to run a raw query. The syntax of the method is as shown below: const [results, metadata] = await sequelize. query( "Your query here", { options } );

What is Sequelize query?

As there are often use cases in which it is just easier to execute raw / already prepared SQL queries, you can use the sequelize. query method. By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows, etc).

How do I log a Sequelize query?

Log SQL Query to the Console Here's how: Sequelize supports the logging option when composing a query object. This configuration overrides the default logging setup in your SQL connector. The logging option expects a log function, like console. log which receives the generates SQL statement.

How do I check if a table exists Sequelized?

you can try to call describeTable of QueryInterface (to get QI call getQueryInterface in sequelize) to get information about a table.


2 Answers

You can't use sequelize directly from require('sequelize'); , first we need to create instance of that , and instance will be created from DB details , so sequelize can know on which platform , which DB it has to run the query

Here is the basic snippet , just for a idea :

const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: 'mysql'|'sqlite'|'postgres'|'mssql',

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },

  // SQLite only
  storage: 'path/to/database.sqlite',

  // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
  operatorsAliases: false
});

sequelize.query("SELECT * from users").then(results => {
    console.log(results);
});

For more detail : DO READ

like image 70
Vivek Doshi Avatar answered Oct 01 '22 00:10

Vivek Doshi


If you are running your db config in external file AND want and extra layer to protect from SQL injection ./database/dbs.js

const Sequelize = require('sequelize')
const db = {}
const sequelize = new Sequelize('dbname', 
                                'username', 
                                'password', {
  host: 'localhost',
  dialect: 'mysql',
  logging: console.log,
  freezeTableName: true,

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
})

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

then in the file you want to use:

const db        = require('../database/db')
...
    const newDbName = 'myNewDb'
    db.sequelize.query(`CREATE DATABASE IF NOT EXISTS ${newDbName} `, {
        type: db.sequelize.QueryTypes.CREATE
    }).then( () => {
        res.send('ok done creating ' + req.body.newDbName).end()
    }).catch( err => {
        res.status(500).send('Err executing command ' + err).end()
    })
like image 9
Michael Nelles Avatar answered Sep 30 '22 23:09

Michael Nelles