Grant Eagon
Grant Eagon

Reputation: 1400

Why is .query undefined on my model in Sails JS console?

In sails console, I should be able to:

User.query("SELECT * FROM USER")

But I get undefined is not a function, meaning .query is undefined.

I've reinstalled/updated sails and waterline, as well as sails globally and I still get the same error.

Thanks!

Upvotes: 1

Views: 1335

Answers (4)

Sanjeev
Sanjeev

Reputation: 1

If you using local.js to set env.

Check env name, it should be small letters.

EX:- environment: process.env.NODE_ENV || 'development'

I got same issue my env name was 'Development', changed to 'development' and working fine.

Upvotes: 0

jhonny lopez
jhonny lopez

Reputation: 325

First you should verify your conection

#model
    module.exports.connections = {
      localMysql: {
        adapter: 'sails-mysql',
        user: 'root',
        host: 'localhost',
        database: 'someDbase'
      },

if you run for example

#EXAMPLE
    User.find().exec(function (error, res){
    console.log(res)
    });  

this return all users, later If you don't run .exec(), your query will not execute.

here you need a callback:

#CONTROLLER
    User.query("SELECT * FROM USER", function(err, results) {
      if (err) 
         return res.serverError(err);
      return res.ok(results);
    });

Upvotes: 0

Grant Eagon
Grant Eagon

Reputation: 1400

Solved it. Sails documentation indicates using:

{
  ...
   adapter: 'sails-postgresql',
  ...
}

But sails-postgresql is still using the older:

{
  ...
   module: 'sails-postgresql',
  ...
}

Once I changed that I started seeing queries come through the server and the query function became defined under the model I created.

Upvotes: 2

DevAlien
DevAlien

Reputation: 2476

How did you configured the model User? Which adapter is it using?

.query method is only allowed when using an SQL adapter.

Upvotes: 1

Related Questions