Node.js - 为什么这段代码不起作用?

时间:2015-06-21 22:03:25

标签: javascript node.js

在搜索数据库时,如果找不到该用户的任何内容,我已尝试使用以下代码执行操作。这是使用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)AND if(!cid)的方法,但这两种方法都不起作用。

1 个答案:

答案 0 :(得分:0)

您的查询是否可能由于某种原因失败并且您没有跟踪错误?

尝试添加.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)});
相关问题