Node.js / sequelize:访问findOne()函数的结果?

时间:2017-01-26 14:25:42

标签: javascript node.js authentication sequelize.js

我是node.js的新手,我尝试从包含(用户名,密码)对的数据库中的用户检索密码。 findOne函数似乎返回一个结果,但是当我尝试访问密码时,我总是有“未定义的”#39;结果是。有人可以帮帮我吗?

这是非常简单的server.js:

var express     = require('express');
var app       = express();
var sqlite    = require('sqlite3');
var port      = process.env.PORT || 8000;

var Sequelize = require('sequelize');
var sequelize = new Sequelize("_databaseName_", "_username_", "_password_", {
  dialect: 'sqlite',
  storage: "users.sqlite"
});

var User = sequelize.define('User',
{
  tx_index: { type: Sequelize.INTEGER, primaryKey: true},
  username: Sequelize.STRING,
  password: Sequelize.STRING
},{
  timestamps: false,
  tableName: 'data',
  freezeTableName: true
})

//no need of sync, as the db already exists and is populated
//User.sync();

User.findOne({where: {username: 'julien'}}).then(function(user) {
   if (!user) {
    console.log('unknown user');
   } else {
    console.log('user password is '+user.password);
    //console.log('user password is '+user.get('password'));
   }
});

app.listen(port);
console.log('listening => http://localhost:' + port);

输出:

node server.js
listening => http://localhost:8000
Executing (default): SELECT `tx_index`, `username`, `password` FROM `data` AS `User` WHERE `User`.`username` = 'julien' LIMIT 1;
user password is undefined

如果我选择其他用户名:

User.findOne({where: {username: 'WHATEVER'}}).then(function(user) {

我有输出:

node server.js
listening => http://localhost:8000
Executing (default): SELECT `tx_index`, `username`, `password` FROM `data` AS `User` WHERE `User`.`username` = 'WHATEVER' LIMIT 1;
unknown user

所以查询似乎没问题,但我无法访问密码(未定义)。

最后,数据库的内容:

SELECT * FROM 'data' LIMIT 0,30;
TX_INDEX    PASSWORD    USERNAME
2           123456      julien
3           titi        toto
4           bar         foo

SELECT tx_index, username, password FROM data WHERE username = 'julien';
TX_INDEX    USERNAME    PASSWORD
2               julien          123456

我疯狂地发现我做错了什么!

:: EDIT ::

当我打印用户对象" console.log(用户);"时,这是输出。它是什么 ?用户'的一个实例模特,但它是空的?

Instance {
  dataValues: {},
  _previousDataValues: {},
  _changed: {},
  '$modelOptions':
   { timestamps: false,
     instanceMethods: {},
     classMethods: {},
     validate: {},
     freezeTableName: true,
     underscored: false,
     underscoredAll: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { username: 'julien' },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: [],
     hooks: {},
     indexes: [],
     name: { plural: 'Users', singular: 'User' },
     omitNul: false,
     tableName: 'data',
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [Object],
        models: [Object],
        modelManager: [Object],
        connectionManager: [Object],
        importCache: {},
        test: [Object],
        queryInterface: [Object] },
     uniqueKeys: {},
     hasPrimaryKeys: true },
  '$options':
   { isNewRecord: false,
     '$schema': null,
     '$schemaDelimiter': '',
     raw: true,
     attributes:
      [ 'tx_index',
        'username',
        'password',
        'generation',
        'reserved1',
        'modified',
        'created',
        'next' ] },
  hasPrimaryKeys: true,
  __eagerlyLoadedAssociations: [],
  isNewRecord: false }

1 个答案:

答案 0 :(得分:0)

所以我终于找到了问题:我的数据库表没有列id,这对于sequelize来说似乎很重要。所以我添加它,即使我不使用它,它也可以工作,而无需修改上面的源代码。