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

时间:2015-07-07 02:27:50

标签: node.js postgresql sails.js waterline

我想问一个问题。我正在使用sails.js和PostgreSQL(使用sails-postgresql模块)开发应用程序。我使用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
        }   
    }
};

我在控制器中的创建功能

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

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

1 个答案:

答案 0 :(得分:1)

试试这个:

模型

    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
  1. 在模型定义中,不存在uuidv4,它在模型配置上。
  2. id也不是有效的属性。
  3. 当您使用Blueprint API时,
  4. actionUtil字段是必填字段,请在node_modules的风帆中res.redirect处查看蓝图挂钩。
  5. 异步时,不要在控制器末尾调用goto。进程,它将在查询尚未完成时给出竞争条件,您将被重定向。
  6. 实际上我不确定它是否会解决您的问题,但您可以尝试并稍后给出结果。