当搜索数据库时没有找到该用户的任何内容时,我尝试使用下面的代码来执行操作。这是使用 twitch-irc 库及其 twitch-irc-db 模块 (Node.js)。
db.where('users', {name: user.username}).then(function(result) {
if (!result) {
console.log('No result lad');
}
var cid = result.items[0].cid;
console.log(result.items[0].cid);
if (!cid) {
db.insert('users', {name: user.username, keys: 0}).then(function() {
console.log(user.username + ' is new to the stream, generating them a blank database entry.');
});
}
});
出于某种原因,上面的 console.log() 行实际上都不起作用。正如你所看到的,我尝试了 if (!result) 和 if (!cid) 的方法,但都不起作用。
最佳答案
是否有可能您的查询由于某种原因失败并且您没有跟踪错误?
尝试添加.fail
db.where('users', {name: user.username}).then(function(result) {
if (!result) {
console.log('No result lad');
}
var cid = result.items[0].cid;
console.log(result.items[0].cid);
if (!cid) {
db.insert('users', {name: user.username, keys: 0}).then(function() {
console.log(user.username + ' is new to the stream, generating them a blank database entry.');
});
}
}).fail(function(err) {console.log(err)});
关于javascript - Node.js - 为什么这段代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969765/