为什么我的 Model.find 什么也没返回?
Why is my Model.find returning nothing?
我对 node 和 sequelize 很陌生,我正在尝试遵循这个 short introduction。
我已经完成了连接到我的数据库 (postgres) 的部分。我还定义了一个模型:
var User = sequelize.define('User', {
username: Sequelize.STRING,
password: Sequelize.STRING
});
我已成功同步方案并创建实例。但是当我尝试使用这个从数据库中读取时:
User
.find({ where: { username: 'john-doe' } })
.then(function(err, johnDoe) {
if (!johnDoe) {
console.log('No user with the username "john-doe" has been found.');
} else {
console.log('Hello ' + johnDoe.username + '!');
console.log('All attributes of john:', johnDoe.get());
}
});
该实例确实存在,但我只看到 'No user with...' 消息。它生成的查询似乎是正确的,当我手动尝试时,返回的结果是我希望看到的。
使用相同的查询我可以做到这一点,这也有效:
sequelize.query("SELECT * FROM my_user_table where username='john-doe'", { type: sequelize.QueryTypes.SELECT})
.then(function(items) {
// We don't need spread here, since only the results will be returned for select queries
console.log(items);
});
我在这里错过了什么?
其实,你离得太近了。但是你不能在 then
方法上使用参数来处理错误。
所以你必须像下面这样使用;
User
.findOne({ where: { username: 'john-doe' } })
.then(function(johnDoe) {
if (!johnDoe) {
console.log('No user with the username "john-doe" has been found.');
} else {
console.log('Hello ' + johnDoe.username + '!');
console.log('All attributes of john:', johnDoe.get());
}
});
您混淆了承诺和节点式回调。通常,当您将回调传递给原始函数时,您只会期望 (err, results)
。如果您调用 then
,您正在使用 promises 并且应该只期望结果。您应该调用 catch
以获取任何错误。
User
.find({ where: { username: 'john-doe' } })
.then(function(johnDoe) {
if (!johnDoe) {
console.log('No user with the username "john-doe" has been found.');
} else {
console.log('Hello ' + johnDoe.username + '!');
console.log('All attributes of john:', johnDoe.get());
}
})
.catch(function(err) {
// Error handling here
});
我对 node 和 sequelize 很陌生,我正在尝试遵循这个 short introduction。
我已经完成了连接到我的数据库 (postgres) 的部分。我还定义了一个模型:
var User = sequelize.define('User', {
username: Sequelize.STRING,
password: Sequelize.STRING
});
我已成功同步方案并创建实例。但是当我尝试使用这个从数据库中读取时:
User
.find({ where: { username: 'john-doe' } })
.then(function(err, johnDoe) {
if (!johnDoe) {
console.log('No user with the username "john-doe" has been found.');
} else {
console.log('Hello ' + johnDoe.username + '!');
console.log('All attributes of john:', johnDoe.get());
}
});
该实例确实存在,但我只看到 'No user with...' 消息。它生成的查询似乎是正确的,当我手动尝试时,返回的结果是我希望看到的。
使用相同的查询我可以做到这一点,这也有效:
sequelize.query("SELECT * FROM my_user_table where username='john-doe'", { type: sequelize.QueryTypes.SELECT})
.then(function(items) {
// We don't need spread here, since only the results will be returned for select queries
console.log(items);
});
我在这里错过了什么?
其实,你离得太近了。但是你不能在 then
方法上使用参数来处理错误。
所以你必须像下面这样使用;
User
.findOne({ where: { username: 'john-doe' } })
.then(function(johnDoe) {
if (!johnDoe) {
console.log('No user with the username "john-doe" has been found.');
} else {
console.log('Hello ' + johnDoe.username + '!');
console.log('All attributes of john:', johnDoe.get());
}
});
您混淆了承诺和节点式回调。通常,当您将回调传递给原始函数时,您只会期望 (err, results)
。如果您调用 then
,您正在使用 promises 并且应该只期望结果。您应该调用 catch
以获取任何错误。
User
.find({ where: { username: 'john-doe' } })
.then(function(johnDoe) {
if (!johnDoe) {
console.log('No user with the username "john-doe" has been found.');
} else {
console.log('Hello ' + johnDoe.username + '!');
console.log('All attributes of john:', johnDoe.get());
}
})
.catch(function(err) {
// Error handling here
});