简体   繁体   English

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

[英]Node.js - Why isn't this code working?

I have tried this code below to do things when there isn't anything found for that user when searching the database. 当在搜索数据库时找不到该用户的任何内容时,我已经尝试过下面的代码来执行操作。 This is using the twitch-irc library with the twitch-irc-db module for it (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.');
        });
    }
});

For some reason, none of the console.log() lines above actually work. 由于某些原因,上面的console.log()行均不起作用。 As you can see, I have tried the method of if (!result) AND if (!cid), neither of these work. 如您所见,我尝试了if(!result)和if(!cid)的方法,但上述两种方法均无效。

Is it possible your query is failing for some reason and you are not tracking the error? 您的查询是否可能由于某种原因而失败并且您没有跟踪错误?

try to add .fail 尝试添加.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)});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM