javascriptnode.jspromiseelectronexecute-script

UnhandledPromiseRejectionWarning: Error: Script failed to execute- Electron execute script


i try to excuteJavaScript in electron

function createTestGmail(username, password){
    username = '12344444444444444444444444asdasd44444444'
    testGmail = new BrowserWindow({
        width: 500, 
        height:300, 
        backgroundColor:'#ccc', 
        title:'Kiểm tra Gmail',
        webPreferences: {
            nodeIntegration: true,
            nativeWindowOpen: true,
        }
    });
    testGmail.loadURL('https://example.com');
    testGmail.webContents.openDevTools();
    testGmail.webContents.executeJavaScript(`
        console.log(`+username+`)    
    `)

    testGmail.on('closed',()=>{
        testGmail = null;
    })
}

if username is a number it work correctly, if username is a string, like below, it show error code

(node:3752) UnhandledPromiseRejectionWarning: Error: Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.
    at WebFrame.<computed> (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\api\web-frame.js:64:33)
    at WebFrame.executeJavaScript (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\common\api\deprecate.js:114:32)
    at C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:11:43
    at C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\ipc-renderer-internal-utils.js:7:40
    at new Promise (<anonymous>)
    at EventEmitter.<anonymous> (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\ipc-renderer-internal-utils.js:7:9)
    at EventEmitter.emit (events.js:200:13)
    at Object.onMessage (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\init.js:42:16)
(node:3752) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3752) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3752) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:3752) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

i have try with tag and it have the same problem


Solution

  • I saw @Bravo answered your question through a comment, but just to improve it, since you're using a template string, you could just:

    testGmail.webContents.executeJavaScript(`console.log('${username}')`) 
    

    It's cleaner that way (since this is the main purpose of a template string, not only allowing to have a multiline text) and you avoid doing string concatenation with the "+" operator.