【问题标题】:Error using sails.js and postgreSQL (using sails-postgresql module)使用sails.js 和postgreSQL 时出错(使用sails-postgresql 模块)
【发布时间】:2015-09-24 08:40:53
【问题描述】:

我想问一个问题。我正在使用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

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

【问题讨论】:

    标签: node.js postgresql sails.js waterline


    【解决方案1】:

    试试这个:

    型号

        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,它在模型配置中。
    2. uuidv4 也不是有效属性。
    3. id 字段在您使用 Blueprint API 时是必需的,请在 node_modules 的风帆中查看 actionUtil 的蓝图挂钩。
    4. 当你有异步时,不要在控制器末尾调用res.redirect。过程中,当查询尚未完成时,它将给出竞争条件,您将被重定向。

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

    【讨论】:

      猜你喜欢
      • 2016-10-02
      • 2014-08-14
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      相关资源
      最近更新 更多