node.jspostgresqlsails.jswaterlinesails-postgresql

sails-postgresql "ReferenceError: User is not defined"


I try to set up postgresql connection with sails.js . Here is my config:

api/models/User.js:

module.exports = {

tableName: 'user',
meta: {
    schemaName: 'public'
},
identity:'sails',
connection: 'postgreSQL',

attributes: {
username:{
    type:'string',
    required:true,
},
password:{
    type:'string',
    required:true,
},
email:{
    type:'email',
    required:true,
},
}};

config/connections.js:

module.exports.connections = {
postgreSQL: {
      adapter: 'sails-postgresql',
      host: '127.0.0.1',
      user: 'sails', // optional
      password: 'pass', // optional
      database: 'sails', //optional
      port:5433,
    }
}

config/models.js

module.exports.models = {
  connection: 'postgreSQL',
  migrate: 'safe',    
};

api/controllers/UserController.js

module.exports = {
    create: function(req,res){
        User.create(req.allParams()).exec(function createdUser(err, created){
            if(created != undefined){
                sails.log('Utilisateur créé')
                res.view('User/user_created');
                sails.log(req.allParams())
            }
            else
            {
                sails.log('Pas d\'utilisateur créé')
                sails.log(req.allParams())
            }

        });
    },

    search: function(req,res){
    sails.log("Recherche saisie:")
    sails.log(req.allParams())
    if(req.allParams() == undefined){
        sails.log("Pas d'utilisateur recherché");
    }
    else {
        User.find({username : {contains: req.allParams().username}}).exec(function (err, userFound){
        sails.log('utilisateur trouvé');
        sails.log(userFound);
        return res.json(userFound);
    })
    }
    }
};

When I lift my app, and i send the following request (which send post agument as params for "create" action from UserController):

http://127.0.0.1:1337/User/Subscribe?username=pierre&password=pass&email=test@test.fr

I have the error:

error: Sending 500 ("Server Error") response: ReferenceError: User is not defined at Object.create (C:\Users\Pierre\Documents\Geek\SAILS\hikout\api\controllers\UserController.js:10:3) at wrapper (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\lodash\lib\index.js:3250:19) at routeTargetFnWrapper (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:181:5) at callbacks (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:164:37) at param (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:138:11) at pass (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:145:5) at nextRoute (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:100:7) at callbacks (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:167:11) at alwaysAllow (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\lib\hooks\policies\index.js:224:11) at routeTargetFnWrapper (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:181:5) at callbacks (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:164:37) at param (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:138:11) at pass (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:145:5) at nextRoute (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:100:7) at callbacks (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:167:11) at module.exports (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\lib\hooks\cors\clear-headers.js:14:3) at routeTargetFnWrapper (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:181:5) at callbacks (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:164:37) at param (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:138:11) at pass (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:145:5) at nextRoute (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:100:7) at callbacks (C:\Users\Pierre\AppData\Roaming\npm\node_modules\sails\node_modules\@sailshq\express\lib\router\index.js:167:11)

However all works perfectly without postgresSQL connection... Does someone know how to solve this ?

Thank you in advance for help.

EDIT: I changed the files path to get the good ones


Solution

  • Actually, the file paths were wrong in my first post. I had set up the right ones in my app.

    The problem was in the file config/models.js. I was trying to run my app with the safe mode for my connection. However, this doesn’t work during the first run. The good way is to use either the alter or drop mode for your connection in order to create tables corresponding to your models during the first run.

    The good config/models.js file is:

    module.exports.models = {
        connection: 'postgreSQL',
        migrate: 'alter',  // you can also use 'drop' 
    };
    

    I also had to change my model to look like so:

    module.exports = {
        attributes: {
            username: {
                type: 'string',
                required: true,
            },
            email: {
                type:'string',
                required:true,
            },
            password: {
                type: 'string',
                required: true,
            },
        }
    };