简体   繁体   English

如何检查string = nodes中的另一个字符串?

[英]How to check if string = another string in node.js?

So I'm using node.js to get info from an API, then sending the response from the API back (mainly to get around cross origin and that fun stuff), so I was wondering how I would check if the response from the API is a certain string? 所以我使用node.js从API获取信息,然后从API发回响应(主要是为了解决交叉起源和那些有趣的东西),所以我想知道如何检查来自API的响应是某个字符串? For example, if you put in an invalid username in the api it returns an empty object, which if you do object.toString() it should be {} (I think?), so I was wondering how I would do an if statement checking if the APIs response is {} (something like if(object.toString() = "{}" { do stuff } ?) 例如,如果你在api中输入一个无效的用户名,它会返回一个空对象,如果你执行object.toString()它应该是{} (我想?),所以我想知道如何做一个if语句检查API响应是否为{} (类似if(object.toString() = "{}" { do stuff } ?)

Here's my code: 这是我的代码:

app.get('/stats/id64/:steamid64', function(httpRequest, httpResponse) {
    var url = '<api link>&steamid=' +
        httpRequest.params.steamid64;
    request.get(url, function(error, steamHttpResponse, steamHttpBody) {
        httpResponse.setHeader('Content-Type', 'application/json');

        var response = {isValidUser: 'false'};

        if(steamHttpBody.toString() == "{}") {
            httpResponse.send(response);
        } else {
            httpResponse.send(steamHttpBody);
        }
    });
});

Any help is appreciated :) 任何帮助表示赞赏:)

I think you would be better checking for an actual empty object, than converting it to a string. 我认为你最好检查一个真正的空对象,而不是将其转换为字符串。 See here: How do I test for an empty JavaScript object? 请参见此处: 如何测试空的JavaScript对象?

But yes, to compare strings in JavaScript, you would typically use === . 但是,是的,要比较JavaScript中的字符串,通常会使用===

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

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