节点js中的两个字符串不相等,但与我相等

时间:2018-08-30 10:50:10

标签: javascript node.js

我不明白发生了什么。我在node js中工作,这是我的代码段:

var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(expectBody != body) {
    console.log("Response body != expected file");
    console.log("Response body: " + body);
    console.log("Expected body: " + expectBody);
}

这是我看到的输出:

Response body != expected file
Response body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}
Expected body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}

据我所知,这些字符串是相同的,但是node js却不这么认为。

我拿起打印出的琴弦并将其保存到两个文件中,然后进行了比较-一无所有!

这与读取文件的方式有关吗?

据我了解,!=用于非严格比较,所以应该只检查变量中的实际文本,对吧?

===更新:===

按照您的建议,我尝试了以下方法:

if(JSON.stringify(expectBody) != JSON.stringify(body)) {
       console.log(" stringify, not equal!");
}

if(expectBody.toString() != body.toString()) {
    console.log(" to string, not equal!");
}

我仍然打印出“ stringify,not equal”和“ to string,not equal”:(

=====已解决:======

这最终对我有用:

var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
    // not equal
}

感谢大家的帮助!

5 个答案:

答案 0 :(得分:1)

此行为的原因是,JavaScript内部具有两种用于验证相等性的方法。

最新的ECMAScript标准定义了七种数据类型:

六种原始数据类型:

  • 布尔
  • 未定义
  • 号码
  • 字符串
  • 符号(ECMAScript 6中的新功能)

然后有一个数据类型为

  • 对象

按价值比较原语,按参考比较对象。

示例01

let a = { "one":"a"};
let b = { "one":"a"};
console.log(a!=b);//true

示例02

let a = { "one":"a"};
let b = a;
console.log(a!=b);//false

在您要比较对象的情况下,您将必须

  • 将对象转换为基本类型(字符串),然后进行比较

  • 编写一个单独的深层对象比较功能(您可以通过简单的Google搜索找到很多功能)

更新-针对您的情况的测试脚本

let a = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]};
let b = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]};

console.log(a!=b); //true
console.log(JSON.stringify(b) != JSON.stringify(b)); //false



// Some other approach to make sure you are not fooled by your eye comparison
var crypto = require('crypto');
console.log(crypto.createHash('md5').update(a).digest("hex"));
console.log(crypto.createHash('md5').update(b).digest("hex"));

答案 1 :(得分:0)

您可以做的是。

let obj1 = JSON.stringify({"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]});
let obj2 = JSON.stringify({"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]});

然后比较它们,obj1 != obj2

就这样

var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(JSON.stringify(expectBody) != JSON.stringify(body)) {
  console.log("Response body != expected file");
  console.log("Response body: " + body);
  console.log("Expected body: " + expectBody);
}

请注意,如果对象的顺序不同,这将失败。

答案 2 :(得分:0)

我希望这会起作用:)

var a = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}

var b = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}


// check your condition

if( JSON.stringify(a) !=  JSON.stringify(b)){
   console.log("Objects are different")
}else{
   console.log("objects are same")
}

答案 3 :(得分:0)

这最终对我有用:

var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
    // not equal
}

感谢大家的帮助!

答案 4 :(得分:0)

我遇到了相同的问题,其中 2 个字符串在呈现到屏幕后一个字母一个字母地相同,但它们一个字节一个字节地略有不同。

通过创建散列,它们确实不同,因此在测试用例中,我必须实现自定义的 stringEquals 而不是使用 assert.equal

// Buffer.from(stringA).toString('hex')

3c703e57656972642d6f3a203c7370616e20636c6173733d226b61746578223e3c7370616e20636c6173733d226b617465782d6d6174686d6c223e3c6d61746820786d6c6e733d22687474703a2f2f7777772e77332e6f72672f313939382f4d6174682f4d6174684d4c223e3c73656d616e746963733e3c6d726f773e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2274727565223e3c6d726f773e3c6d6f2066656e63653d2274727565223e283c2f6d6f3e3c6d7461626c6520726f7773706163696e673d22302e31363030656d2220636f6c756d6e616c69676e3d2263656e7465722063656e7465722220636f6c756d6e73706163696e673d2231656d223e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d6e3e313c2f6d6e3e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c2f6d7461626c653e3c6d6f2066656e63653d2274727565223e293c2f6d6f3e3c2f6d726f773e3c2f6d7374796c653e3c2f6d726f773e3c616e6e6f746174696f6e20656e636f64696e673d226170706c69636174696f6e2f782d746578223e5c646973706c61797374796c657b5c626567696e7b706d61747269787d205c242026616d703b20315c5c5c24205c656e647b706d61747269787d7d3c2f616e6e6f746174696f6e3e3c2f73656d616e746963733e3c2f6d6174683e3c2f7370616e3e3c7370616e20636c6173733d226b617465782d68746d6c2220617269612d68696464656e3d2274727565223e3c7370616e20636c6173733d2262617365223e3c7370616e20636c6173733d22737472757422207374796c653d226865696768743a322e3430303033656d3b766572746963616c2d616c69676e3a2d302e3935303033656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d696e6e6572223e3c7370616e20636c6173733d226d6f70656e2064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e283c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d7461626c65223e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d7420766c6973742d7432223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e207374796c653d22746f703a2d322e34303939393939393939393939393937656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d73223ee2808b3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a302e39353030303030303030303030303034656d3b223e3c7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d74223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e313c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d636c6f73652064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e293c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e2e3c2f703e0a

// Buffer.from(stringB).toString('hex')

3c703e57656972642d6f3a203c7370616e20636c6173733d226b61746578223e3c7370616e20636c6173733d226b617465782d6d6174686d6c223e3c6d61746820786d6c6e733d22687474703a2f2f7777772e77332e6f72672f313939382f4d6174682f4d6174684d4c223e3c73656d616e746963733e3c6d726f773e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2274727565223e3c6d726f773e3c6d6f2066656e63653d2274727565223e283c2f6d6f3e3c6d7461626c6520726f7773706163696e673d22302e31363030656d2220636f6c756d6e616c69676e3d2263656e7465722063656e7465722220636f6c756d6e73706163696e673d2231656d223e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d6e3e313c2f6d6e3e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c2f6d7461626c653e3c6d6f2066656e63653d2274727565223e293c2f6d6f3e3c2f6d726f773e3c2f6d7374796c653e3c2f6d726f773e3c616e6e6f746174696f6e20656e636f64696e673d226170706c69636174696f6e2f782d746578223e5c646973706c61797374796c657b5c626567696e7b706d61747269787d205c242026616d703b20315c5c5c24205c656e647b706d61747269787d7d3c2f616e6e6f746174696f6e3e3c2f73656d616e746963733e3c2f6d6174683e3c2f7370616e3e3c7370616e20636c6173733d226b617465782d68746d6c2220617269612d68696464656e3d2274727565223e3c7370616e20636c6173733d2262617365223e3c7370616e20636c6173733d22737472757422207374796c653d226865696768743a322e3430303033656d3b766572746963616c2d616c69676e3a2d302e3935303033656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d696e6e6572223e3c7370616e20636c6173733d226d6f70656e2064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e283c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d7461626c65223e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d7420766c6973742d7432223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e207374796c653d22746f703a2d322e34303939393939393939393939393937656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d73223e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a302e39353030303030303030303030303034656d3b223e3c7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d74223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e313c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d636c6f73652064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e293c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e2e3c2f703e0a