如何检查string= node.js中的另一个string?
所以我使用node.js从API获取信息,然后从API发回响应(主要是为了避免交叉来源和有趣的东西),所以我想知道如何检查API的响应是一定的string? 例如,如果你在api中input一个无效的用户名,它将返回一个空的对象,如果你做object.toString()
它应该是{}
(我想?),所以我想知道如何做if语句检查API响应是否为{}
(类似于if(object.toString() = "{}" { do stuff }
?)
这是我的代码:
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); } }); });
任何帮助表示赞赏:)
我想你会更好地检查一个实际的空对象,而不是将其转换为string。 看到这里: 我如何testing一个空的JavaScript对象?
但是,是的,要比较JavaScript中的string,通常使用===
。