P
P
ptvalx2020-06-18 13:13:30
Node.js
ptvalx, 2020-06-18 13:13:30

Why does an error pop up inside the osmtogeojson library when using it in preload (Electron)?

preload.js code:

spoiler
const Request = require('request-promise');
const OSMToGeoJSON = require('osmtogeojson');
const DOMParser = require('xmldom').DOMParser;
const Log = console.log;

Request({
    method: 'GET',
    uri: 'https://api.openstreetmap.org/api/0.6/map?bbox=53.97501,57.05107,53.97616,57.05166',
    encoding: 'utf8'
}).then(function (xml_osm) {
    var xml_parser = new DOMParser();
    var xml = xml_parser.parseFromString(xml_osm);
    var geojson = OSMToGeoJSON(xml);
    var json_geojson = JSON.stringify(geojson);
    Log(json_geojson);
}).catch(function (err) {
    Log(err);
});

Error image:
spoiler
5eeb3c364025e779565117.png

Part of main.js code:
spoiler
const {app, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      //nodeIntegration:true
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
   mainWindow.webContents.openDevTools()
}


I execute this code in Electron in the main thread (main.js) at the end of the file - everything works. I execute in preload.js - an error occurs in the osmtogeojson library: property length = undefined. While the rest of the code is running, data is being downloaded from the internet. I'm not sure about xmldom, but it doesn't throw any errors. I tried to change the names of the constants in the main process - it did not help. There is no other code in preload.js. I need to use this code in "render". How to push it there so that it does not cause errors? If I include libraries in preload, and call the code in rendering, the error is the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ptvalx, 2020-06-18
@ptvalx

Began to use parseXML from jquery - the problem is gone. But it worked several times slower than xmldom when I used it all through the console. I don’t know how it will be now when there is a real DOM and jquery is connected as it is usually connected on sites.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question