gpt4 book ai didi

javascript - Node.js/sequelize : Accessing the result of findOne() function?

转载 作者:行者123 更新时间:2023-12-03 22:41:36 24 4
gpt4 key购买 nike

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

这是非常简单的 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

我要发疯了,发现我做错了什么!

::编辑::

当我打印 wole 用户对象“console.log(user);”时,这是输出。它是什么 ? “用户”模型的一个实例,但它是空的?
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 }

最佳答案

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

关于javascript - Node.js/sequelize : Accessing the result of findOne() function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41875668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com