sails-postgresql "ReferenceError: User is not defined"
sails-postgresql "ReferenceError: User is not defined"
我尝试用 sails.js 建立 postgresql 连接。这是我的配置:
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);
})
}
}
};
当我打开我的应用程序时,我发送了以下请求(发送 post 参数作为来自 UserController 的 "create" 操作的参数):
http://127.0.0.1:1337/User/Subscribe?username=pierre&password=pass&email=test@test.fr
我有错误:
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)
然而,在没有 postgresSQL 连接的情况下,一切都可以完美运行...有人知道如何解决这个问题吗?
提前感谢您的帮助。
编辑:我更改了文件路径以获得好的文件
您似乎缺少用户 table 的模型文件。
应该在api/models/User.js
我认为您只需移动文件 api/controllers/User.js
即可使其正常工作。
还将您的 api/controller
文件夹重命名为 api/controllers
Sails 是基于约定的,如果你把东西放在正确的地方,它就会毫不费力地工作。所以你的模型文件需要进去
api/models/TABLENAME.js
api/controllers/TABLENAMEController.js
如果这两个文件都存在,它将创建 table 并为您声明全局变量 TABLENAME。
实际上,我的第一个文件路径是错误的 post。我在我的应用程序中设置了正确的。
问题出在文件 config/models.js
中。我试图 运行 我的应用程序使用 safe
模式进行连接。但是,这在第一个 运行 期间不起作用。好的方法是为您的连接使用 alter
或 drop
模式,以便在第一个 运行.
期间创建与您的模型对应的表
好的config/models.js
文件是:
module.exports.models = {
connection: 'postgreSQL',
migrate: 'alter', // you can also use 'drop'
};
我还必须将我的模型更改为如下所示:
module.exports = {
attributes: {
username: {
type: 'string',
required: true,
},
email: {
type:'string',
required:true,
},
password: {
type: 'string',
required: true,
},
}
};
我尝试用 sails.js 建立 postgresql 连接。这是我的配置:
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);
})
}
}
};
当我打开我的应用程序时,我发送了以下请求(发送 post 参数作为来自 UserController 的 "create" 操作的参数):
http://127.0.0.1:1337/User/Subscribe?username=pierre&password=pass&email=test@test.fr
我有错误:
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)
然而,在没有 postgresSQL 连接的情况下,一切都可以完美运行...有人知道如何解决这个问题吗?
提前感谢您的帮助。
编辑:我更改了文件路径以获得好的文件
您似乎缺少用户 table 的模型文件。
应该在api/models/User.js
我认为您只需移动文件 api/controllers/User.js
即可使其正常工作。
还将您的 api/controller
文件夹重命名为 api/controllers
Sails 是基于约定的,如果你把东西放在正确的地方,它就会毫不费力地工作。所以你的模型文件需要进去
api/models/TABLENAME.js
api/controllers/TABLENAMEController.js
如果这两个文件都存在,它将创建 table 并为您声明全局变量 TABLENAME。
实际上,我的第一个文件路径是错误的 post。我在我的应用程序中设置了正确的。
问题出在文件 config/models.js
中。我试图 运行 我的应用程序使用 safe
模式进行连接。但是,这在第一个 运行 期间不起作用。好的方法是为您的连接使用 alter
或 drop
模式,以便在第一个 运行.
好的config/models.js
文件是:
module.exports.models = {
connection: 'postgreSQL',
migrate: 'alter', // you can also use 'drop'
};
我还必须将我的模型更改为如下所示:
module.exports = {
attributes: {
username: {
type: 'string',
required: true,
},
email: {
type:'string',
required:true,
},
password: {
type: 'string',
required: true,
},
}
};