返回

node.js-从获取数据工作中过滤出空值?

发布时间:2022-05-11 00:32:53 318
# 数据库# sql# json# 数据

我正在尝试提取数据,而不是将数据插入数据库。但由于某种原因,我的代码无法过滤掉空值。一切正常,直到const test=sqlinsert。过滤器(e=gt;e!==null && e!==undefined);我只是出于某种原因过滤掉了空值?

async function fetchSources() {
  fetch(
    "https://frost.met.no/sources/v0.jsonld?types=SensorSystem&country=NO",
    {
      method: "get",
      body: JSON.stringify(),
      headers: {
        Authorization:
          "Basic YjVlNmEzODEtZmFjNS00ZDA4LWIwNjktODcwMzBlY2JkNTFjOg==",
      },
    }
  )
    .then((res) => res.json())
    .then(async (data) => {
      try {
        await db.query("DROP TABLE IF EXISTS sources;");
        await db.query(
          `CREATE TABLE sources(
              source_id VARCHAR(10) PRIMARY KEY UNIQUE NOT NULL,
              name VARCHAR(60) NOT NULL,
              shortName VARCHAR(50),country VARCHAR(70) NOT NULL,
              countryCode VARCHAR(80),
              masl INT NOT NULL,
              long VARCHAR(90) NOT NULL,
              lat VARCHAR(100) NOT NULL,
              geog geography(point) NOT NULL,
              valid_from TIMESTAMP,
              county VARCHAR(100),
              countyId INT,
              municipality VARCHAR(100) ,
              municipalityId INT);`
        );
        let sqlinsert= data.data.map((source)=>{
          if (source.geometry) {
            if(!source.masl){
              source.masl=0
          }
          let Point = `POINT(${source.geometry.coordinates[1]} ${source.geometry.coordinates[0]})`;
          return `('${source.id}', '${source.name}', '${source.shortName}','${source.country}','${source.countryCode}',${source.masl},'${source.geometry.coordinates[0]}','${source.geometry.coordinates[1]}','${Point}','${source.validFrom}','${source.county}',${source.countyId},'${source.municipality}',${source.municipalityId})`
          }
        })
        const test = sqlinsert.filter(e=>e!==null && e!=="undefined");
        console.log(test[45])
        //const result = await db.query("INSERT INTO sources(source_id,name,shortName,country,countryCode,masl,long,lat,geog,valid_from,county,countyId,municipality,municipalityId) values"+sqlinsert.filter((e)=>e!==null))
        //INSERT INTO sources(source_id,name,shortName,country,countryCode,masl,long,lat,geog,valid_from,county,countyId,municipality,municipalityId) values('SN47230', 'AKRA UNGDOMSSKOLE', 'Akra ','Norge','NO',18,'5.1963','59.2555',POINT(59.2555,5.1963),'2013-10-29T00:00:00.000Z','ROGALAND',11,'KARMOY',1149)
        console.log("happend")
        //fs.writeFileSync('./data6.json', JSON.stringify(data.data, null, 2), 'utf-8');
        return ("sucsess");
      } catch (err) {
        console.log(err);
      }
    });
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像