Sequelize query is giving TypeError: undefined is not a function
In my case, I was reading the contents of a file and importing it in a migration with:
const sqlFile = fs.readFileSync(file, {encoding: "UTF-8"});
await queryInterface.sequelize.query(sqlFile, {
raw: true,
type: Sequelize.QueryTypes.RAW
});
But I was receiving the error:
Error importing: sql.trim is not a function
.
Long story short, my SQL contained the ` (backquote/backtick) character, and I needed to escape it.
So I escaped it as soon as I read the file:
const sqlFile = fs.readFileSync(file, {encoding: "UTF-8"}).replace('`', '\`');
Problem solved. But it took me a bit over an hour to figure that out.
You can use
const sql = "select * from ..."
model.sequelize.query(sql, { type: model.sequelize.QueryTypes.SELECT })
.then(function (rows) {
... do a job on the query here...
})
You need to be calling query()
on a Sequelize
instance instead:
var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password');
sequelize.query("SELECT * FROM 'property'", { type:Sequelize.QueryTypes.SELECT})
.then(function(properties) {
res.json(properties)
})