【发布时间】:2020-02-27 15:05:17
【问题描述】:
我正在使用AISHub APIs 构建一个船只可视化器。在查询 API 后,我能够获得一个包含数千个容器的 json 文件,但我只过滤我感兴趣的容器,并将它们注入到网页上的表格中。 API 提供以下文件:[NAME, MMSI, LONGITUDE, LATITUDE, others...]。我用于过滤的最重要参数是:NAME 和 MMSI(虽然可能有多个容器具有相同的NAME,但不能有两个容器具有相同的MMSI 编号,因为它是唯一的)。
问题我的问题是filter 函数似乎没有正确的行为。
事实上,它不会针对特定的NAME 和/或MMSI 进行唯一过滤,我最终会拥有多个具有相同NAME 和不同MMSI 的容器。还有应该出现的容器,它没有,尽管我为该特定容器硬编码了NAME 和MMSI。
这是无法解释的,因为我将这些数字硬编码为专门过滤。
在我用于过滤搜索的代码下方:
var express = require('express');
var router = express.Router();
var axios = require('axios');
const NodeCache = require('node-cache');
const myCache = new NodeCache();
let hitCount = 0;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
const mmsiOfInterest = [
'367029520', // ok -> MICHIGAN
'366909730', // ok -> JP BOISSEAU
'367128570', // ok -> DELAWARE BAY
'366744010', // ok -> ATLANTIC SALVOR
// Other MMSI numbers .....
];
const shipNamesOfInterest = [
'MICHIGAN',
'JP BOISSEAU',
'DELAWARE BAY',
'ATLANTIC SALVOR',
// Other NAMES....
]
router.get('/hello', async function(req, res, next) {
const allData = myCache.get('allData');
if (!allData) {
hitCount++;
console.log(`hit ${hitCount} number of times`);
const { data } = await axios.get(
'http://data.aishub.net/ws.php?username=MY_KEY&format=1&output=json&compress=0&latmin=11.42&latmax=58.20&lonmin=-134.09&lonmax=-52.62'
);
const [ metaData, ships ] = data;
console.log(data);
const shipsOfInterest = ships.filter(
(ship) => mmsiOfInterest.includes(ship.MMSI) || shipNamesOfInterest.includes(ship.NAME)
);
myCache.set('allData', shipsOfInterest, 70);
res.send(data);
return;
}
console.log('this is the data:', allData);
res.send(allData);
});
module.exports = router;
同样低于来自 API 的典型 JSON 响应:
{"MMSI":225342000,"TIME":"2020-01-26 01:45:48 GMT","LONGITUDE":1.43912,"LATITUDE":38.91523,"COG":339.9,"SOG":0,"HEADING":297,"ROT":0,"NAVSTAT":0,"IMO":9822449,"NAME":"ILLETAS JET","CALLSIGN":"EAVX","TYPE":40,"A":4,"B":25,"C":4,"D":4,"DRAUGHT":0,"DEST":"IBIZA","ETA":"00-00 00:00"}
到目前为止我做了什么:
1) 我尝试了与filter 函数的不同组合,并尝试按MMSI 进行过滤,这应该对每艘船都是唯一的,但我最终还是得到了具有相同NAME 和不同@ 的船987654341@(尽管我硬编码了MMSI...我不明白):
const shipsOfInterest = ships.filter(
(ship) => mmsiOfInterest.includes(ship.MMSI)
);
在我尝试通过NAME 过滤后,但这也不起作用:
const shipsOfInterest = ships.filter(
(ship) => shipNamesOfInterest.includes(ship.NAME)
);
编辑 2
router.get('/hello', async function(req, res, next) {
//
const allData = myCache.get('allData');
if (!allData) {
hitCount++;
console.log(`hit ${hitCount} number of times`);
const { data } = await axios.get(
'http://data.aishub.net/ws.php?username=KEY&format=1&output=json&compress=0&latmin=11.42&latmax=58.20&lonmin=-134.09&lonmax=-52.62'
);
// console.log(data);
const { metaData, ships } = data;
const set = new Set();
const shipsOfInterest = ships.filter((ship) => {
if (set.has(ship.MMSI)) return false;
set.add(ship.MMSI);
return true;
});
myCache.set('allData', shipsOfInterest, 70);
res.send(data);
return;
}
console.log('this is the data:', allData);
res.send(allData);
});
module.exports = router;
下面的错误:
我不知道如果以不同的方式组织filter 函数是否可以获得更好的结果。我认为这可能是组织搜索的一种非常好的方法,但不明白它不起作用的地方。
感谢您指出解决此问题的正确方向。
【问题讨论】:
-
通过“对 MMSI 进行硬编码,您是说类似以下代码的内容返回带有多个 MMSI 值的内容?
const shipsOfInterest = ships.filter(ship => ship.MMSI === '367029520' ); -
另外,您能否提供一个 API 返回的 JSON 的小示例?他们的网站只显示了一个 XML 格式的示例。
-
我没有直接尝试,因为我对关联图进行了硬编码:
mmsiOfInterest与shipNamesOfInterest完全匹配。不过我可以试试。另外是的,让我发布一个JSON格式的典型示例。 -
好的,发布
JSON回复 -
出现新错误时,如果 API 返回错误代码,您应该尝试将
get包装在“try/catch”块中以停止执行。
标签: javascript json reactjs ais