Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'join' of undefined #9

Open
DaNgLiN opened this issue Jan 25, 2020 · 2 comments
Open

Cannot read property 'join' of undefined #9

DaNgLiN opened this issue Jan 25, 2020 · 2 comments

Comments

@DaNgLiN
Copy link

DaNgLiN commented Jan 25, 2020

at get_weather (weather.js:10)
at HTMLButtonElement.onclick (weather.html:21)
Am getting this error help me folks

@NouamaneTazi
Copy link

Make sure you add nodeIntegration: true to your mainWindow by adding :

const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } })

to createWindow() inside main.js

@Crowmium1
Copy link

What I did in 2024 to get get_weather() to work. Comments can only be so long, so will need to fix the spacing and indentation.

Make sure you are pointing to the right version of python. I was pointing at a version which didn't even have pip installed, which gave me undefined errors. I used the IPC communication method instead of using Rest APIs.

Keep in mind these tips:
Do Not Use 'require' in renderer Process: Since nodeIntegration is disabled and contextIsolation is enabled.
To use the exposed API, interact with the main process using window.electronAPI.
If electronAPI is not properly exposed to the renderer process, verify the preload script's execution, ensuring the paths and configurations are correct.

I used this to initialize the main.js:

const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
const { PythonShell } = require('python-shell');
let mainWindow

And ended the script with code like this:

ipcMain.handle('get-weather', async (event, city) => {
console.log(Received get-weather request for city: ${city}); // It doesn't show, but use single quotes here
return new Promise((resolve, reject) => {
let options = {
scriptPath: path.join(__dirname, 'engine'),
pythonPath: 'C:/Users/ljfit/AppData/Local/Programs/Python/Python312/python.exe',
args: [city],
encoding: 'utf8'}

PythonShell.run('weather_engine.py', options, function (err, results) {
if (err) {
console.error('Error executing Python script:', err);
reject(err);
} else {
resolve(results.join('\n')); }})})})

In weather_engine.py I changed sys.arv[0] to:

if len(sys.argv) > 1:
city = sys.argv[1]
else:
city = input("Please enter the city name: ")

def get_weather(place):
(etc..)

In weather.html I inserted a function that began like this:

<script> console.log('window.electronAPI:', window.electronAPI); // Outputs the electronAPI object function get_weather() { var city = document.getElementById("city").value; if (!city) { swal("Please enter a city name."); return; } console.log('get_weather called with city:', city); if (window.electronAPI && window.electronAPI.getWeather) { window.electronAPI.getWeather(city) .then(message => { swal(`Weather in ${city}`, message); document.getElementById("city").value = ""; }) (etc...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants