Reputation: 119
I want to ask a question. I'm developing an app using sails.js and PostgreSQL (using sails-postgresql module). I'm using UUID for my primary key type instead of integer. But there are some error when I try to insert a data to my database.
my model UserModel.js
var uuid = require('node-uuid');
module.exports = {
adapter: 'somePostgresqlServer',
autoPK: false,
migrate: 'safe',
attributes: {
ID: {
primaryKey: true,
type: 'string',
defaultsTo: function (){
return uuid.v4();
},
unique: true,
index: true,
uuidv4: true
},
username: {
type: 'string',
required: true,
unique: true
}
}
};
my create function in the controller
create: function(req, res) {
if (!req.param('_username') || !req.param('_newPassword') ||
!req.param('_confirmPassword') || !req.param('_emailAddress') ||
!req.param('_firstName') || !req.param('_lastName')) {
var errorMessage = ["All field are required to sign up"];
req.session.flash = {
err : errorMessage
}
res.redirect('/login');
return;
}
if (req.param('_newPassword') != req.param('_confirmPassword')) {
var errorMessage = ["New password and confirm password must be same"];
req.session.flash = {
err : errorMessage
}
res.redirect('/login');
return;
}
UserModel.create({
username: req.param('_username'),
encryptedPassword: req.param('_newPassword'),
emailAddress: req.param('_emailAddress'),
firstName: req.param('_firstName'),
lastName: req.param('_lastName')
}).exec(function(err,post) {
if (err) {
return res.error();
}
res.redirect('/');
})
res.redirect('/');
}
the error
/home/***/***/***/node_modules/sails-postgresql/lib/adapter.js:393
Object.keys(collection.schema).forEach(function(schemaKey) {
^
TypeError: Object.keys called on non-object
at Function.keys (native)
at __CREATE__ (/home/***/***/***/node_modules/sails-postgresql/lib/adapter.js:393:16)
at after (/home/***/***/***/node_modules/sails-postgresql/lib/adapter.js:1155:7)
at /home/***/***/***/node_modules/sails-postgresql/lib/adapter.js:1049:7
at /home/***/***/***/node_modules/sails-postgresql/node_modules/pg/lib/pool.js:77:9
at dispense (/***/***/***/node_modules/sails-postgresql/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:250:16)
at Object.me.acquire (/home/***/***/***/node_modules/sails-postgresql/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:319:5)
at Object.pool.connect (/home/***/***/***/node_modules/sails-postgresql/node_modules/pg/lib/pool.js:71:12)
at PG.connect (/home/***/***/***/node_modules/sails-postgresql/node_modules/pg/lib/index.js:49:8)
at spawnConnection (/home/***/***/***/node_modules/sails-postgresql/lib/adapter.js:1048:8)
at Object.module.exports.adapter.create (/home/***/***/***/node_modules/sails-postgresql/lib/adapter.js:361:7)
at module.exports.create (/usr/lib/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:84:13)
at bound.createValues (/usr/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:214:16)
at /usr/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:74:20
at /usr/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:708:13
at /usr/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:49:16
I wish you can help me. Thanks for your attention :)
Upvotes: 1
Views: 784
Reputation: 2051
Try this:
var uuid = require('node-uuid');
module.exports = {
adapter: 'somePostgresqlServer',
autoPK: false,
attributes: {
id: {
primaryKey : true,
type : 'string',
defaultsTo : function (){
return uuid.v4();
},
unique : true,
index : true
},
username: {
type : 'string',
required : true,
unique : true
}
}
};
create: function(req, res) {
var username = req.param('_username'),
newPassword = req.param('_newPassword'),
confirmPassword = req.param('_confirmPassword'),
emailAddress = req.param('_emailAddress'),
firstName = req.param('_firstName'),
lastName = req.param('_lastName');
if (!(username || newPassword || confirmPassword || emailAddress || firstName || lastName)) {
var errorMessage = ["All field are required to sign up"];
req.session.flash = {
err : errorMessage
}
return res.redirect('/login');
}
if (newPassword != confirmPassword) {
var errorMessage = ["New password and confirm password must be same"];
req.session.flash = {
err : errorMessage
}
return res.redirect('/login');
}
UserModel
.create({
username : username,
encryptedPassword : newPassword,
emailAddress : emailAddress,
firstName : firstName,
lastName : lastName
})
.then(function(post) {
res.redirect('/');
})
.catch(res.negotiate);
}
migrate
, it's on model configuration.uuidv4
also not a valid attribute.id
field is necessary while you use Blueprint API
, look after blueprint hooks at actionUtil
in your sails at node_modules
.res.redirect
at the end of controller while you have async. process, it will give race condition when query not completed yet and you will get redirected.Actually I don't pretty sure if it will solve your problem, but you can try it and give a result later.
Upvotes: 1