Sequelize:无法读取未定义的属性

时间:2017-04-04 23:19:24

标签: javascript mysql node.js sequelize.js

我在使用Sequelize时遇到错误,无法弄清问题是什么。

我正在创建一个新记录但是在创建记录之后它会返回结果,然后捕获一个异常并说明该值是未定义的。记录已创建但返回错误。我不确定为什么会这样。

这是我的sql表



user_registration = conn.sequelize.define('user_registration',{
    idUserRegistration:{
      type: Sequelize.INTEGER(11).UNSIGNED,
      primaryKey: true,
      autoIncrement:true
    },
    email:{
      type: Sequelize.STRING
    },
    firstName:{
      type: Sequelize.STRING
    },
    lastName:{
      type: Sequelize.STRING
    },
    guid:{
      type: Sequelize.UUID,
      defaultValue: Sequelize.UUIDV1
    },
    createDate:{
      type: Sequelize.DATE, defaultValue: Sequelize.NOW
    }
  },{
   freezeTableName: true
 }
)




这是执行语句

的脚本



var 
 conn = require('../../mysql/mysqlConn')
,sequelize = require('sequelize')
,mysql = require('mysql')
,userRegistration = require('../../mysql/tables').user_registration

var createAccount = function(user,callback){

    console.log('create user:',user.email)

    var results = {}

    try{

        userRegistration.create({
            email: user.email, 
            firstName: user.firstName, 
            lastName: user.lastName
        })
        .then(function(userRes){ 
            console.log('userRegistration Results:',userRes)
            results.error = false
            results.results = userRes
            callback(results)    
        })
        .catch(function(err){
            console.log('Caught error in createRegistration!')
            console.log(err)
            results.error = true
            results.results = err
            callback(results)
        })

    }catch(e){
        console.log('EXCEPTION: error in createRegistration')
        console.log(e)
        results.error = true
        results.results = 'Error occurred creating user registration:',e
        callback(results)
    }
    
}

exports.createAccount = createAccount




以下是遇到错误的结果



create user: jsmith@me.com
Executing (default): INSERT INTO `user_registration` (`idUserRegistration`,`email`,`firstName`,`lastName`,`guid`,`create
Date`) VALUES (DEFAULT,'jsmith@me.com','John','Smith','a6193d70-198b-11e7-b0f0-93c15705c5db','2017-04-04 23:08:43');
userRegistration Results: Instance {
  dataValues:
   { guid: 'a6193d70-198b-11e7-b0f0-93c15705c5db',
     createDate: 2017-04-04T23:08:43.000Z,
     idUserRegistration: 10,
     email: 'jsmith@me.com',
     firstName: 'John',
     lastName: 'Smith' },
  _previousDataValues:
   { email: 'jsmith@me.com',
     firstName: 'John',
     lastName: 'Smith',
     idUserRegistration: 10,
     guid: 'a6193d70-198b-11e7-b0f0-93c15705c5db',
     createDate: 2017-04-04T23:08:43.000Z },
  _changed:
   { email: false,
     firstName: false,
     lastName: false,
     idUserRegistration: false,
     guid: false,
     createDate: false },
  '$modelOptions':
   { timestamps: false,
     instanceMethods: {},
     classMethods: {},
     validate: {},
     freezeTableName: true,
     underscored: false,
     underscoredAll: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { email: 'jsmith@me.com' },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: [],
     hooks: {},
     indexes: [],
     name: { plural: 'user_registrations', singular: 'user_registration' },
     omitNul: false,
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [Object],
        models: [Object],
        modelManager: [Object],
        connectionManager: [Object],
        importCache: {},
        test: [Object],
        queryInterface: [Object] },
     uniqueKeys: {},
     hasPrimaryKeys: true },
  '$options':
   { isNewRecord: true,
     '$schema': null,
     '$schemaDelimiter': '',
     attributes: undefined,
     include: undefined,
     raw: undefined,
     silent: undefined },
  hasPrimaryKeys: true,
  __eagerlyLoadedAssociations: [],
  isNewRecord: false }
Caught error in createRegistration!
TypeError: Cannot read property 'email' of undefined     




有什么想法吗?

0 个答案:

没有答案