使用sails.js和postgreSQL时出错(使用sails-postgresql模块)

我想问个问题。 我正在开发一个应用程序使用sails.js和PostgreSQL(使用sails-postgresql模块)。 我使用的是我的主键types而不是整数的UUID。 但是当我尝试向我的数据库插入数据时有一些错误。

我的模型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 } } }; 

我在控制器中创buildfunction

 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('/'); } 

错误

 /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 

我希望你能帮助我。 感谢您的关注 :)

尝试这个:

模型

  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); } 
  1. 在模型定义上,不存在migrate ,这是模型configuration。
  2. uuidv4也不是一个有效的属性。
  3. 在使用Blueprint APIid字段是必要的,请在actionUtil中查看actionUtil中的蓝图钩子。
  4. res.redirect时不要在控制器末尾调用res.redirect 。 过程中,当查询还没有完成时,会出现竞争状态,您将被redirect。

其实我不确定它是否能解决你的问题,但是你可以试一下,稍后再给出结果。