簡體   English   中英

在測試中使用時,來自 NodeJS webservice 的布爾值不是布爾值

[英]Boolean value from NodeJS webservice is not boolean when using in test

我調用了 NodeJS 網絡服務:

request({
                url: "http://10.210.210.41:3001/trackable/lastPos",
                json: true
            }, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    async.forEachOf(body, function (vehiculeJSON, cleJSON, cbVehiculeJSON) {
                        var tabFlotte = vehiculeJSON.flotte;
                        if (tabFlotte.length > 0) {
                            var dbJSON = rows.find(function(row) {
                                return row.num_tag_ip == cleJSON;
                            });
                            if (dbJSON != undefined) {
                                var num_tag_ip = cleJSON, etat = vehiculeJSON.state, coordinates = vehiculeJSON.position, immatriculation = dbJSON.immatriculation, lib = dbJSON.lib, msisdn = dbJSON.msisdn;
                                if (vehiculeJSON.is_outofarea == true) { // here is the test
                                    ...
                                }
                            }
                        }
                        cbVehiculeJSON();
                    }, function () {
                        ...
                    });
                } else {
                    ...
                }
            });

它返回這樣的值:

{
    "TMA0224": {
        "zone": 161,
        "fixed_at": "2019-12-03T09:55:49.000Z",
        "flotte": [
            {
                "type": "fleet",
                "id": "GAN.TELMAPROPRE"
            },
            {
                "type": "fleet",
                "id": "TMA"
            },
            {
                "type": "fleet",
                "id": "TMA.DVI-MOTOS"
            },
            {
                "type": "fleet",
                "id": "TMA.TELMA"
            }
        ],
        "state": "MOV",
        "numero_tag_ip": "TMA0224",
        "immatriculation": "1271TBH",
        "ignition": false,
        "mileage": 3263,
        "heading": 313,
        "speed": 2,
        "is_outofarea": true,
        "position": {
            "latitude": -18.90895,
            "longitude": 47.536675
        }
    },
    ...
  }

但是在運行時,測試永遠不會進入“if”測試。 但是當我用vehiculeJSON.is_outofarea == "true"替換測試時,測試成功了! 那么為什么 json 假定的布爾值轉換為 String ?

試試這個:

if (!!vehiculeJSON.is_outofarea) {
    ...
}

無論它是string還是bool它都應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM