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

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

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.

Sometimes you will mess with single quote and quote mark.
I made an util function to log everywhere.
function logEverywhere(mainWindow, message) {
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.executeJavaScript(`console.log(\`${message}\`)`);
}
}

I was taking a UDEMY course called Master Electron: Desktop Apps with HTML, JavaScript & CSS, by Ray Viljoen
And in Lesson 3.17. Session: DownloadItem, I got this same error:
(node:9552) UnhandledPromiseRejectionWarning: Error: Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.
at WebFrame.e.startsWith.e.startsWith.WebFrame.<computed> [as _executeJavaScript] (electron/js2c/renderer_init.js:87:1542)
at electron/js2c/renderer_init.js:139:429
at electron/js2c/renderer_init.js:123:361
at EventEmitter.<anonymous> (electron/js2c/renderer_init.js:127:872)
at EventEmitter.emit (events.js:223:5)
at Object.onMessage (electron/js2c/renderer_init.js:115:818)
(node:9552) 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:9552) [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.
In my case I tracked the bug/problem down to some code that had a divide by zero in the event handler for DownloadItem.on. The code snippet in question is:
ses.on('will-download', (e, downloadItem, webContents) => {
let fileName = downloadItem.getFilename()
let fileSize = downloadItem.getTotalBytes() // this was zero (which caused the bug)
downloadItem.on('updated', (e, state) => {
let received = downloadItem.getReceivedBytes()
if (state === 'progressing' && received) {
// THE NEXT LINE CAUSED the divide by zero exception because fileSize was zero.
let progress = Math.round((received/fileSize)*100)
webContents.executeJavaScript(`window.progress.value = ${progress}`)
}
})
})
So my takeaway is that Electron's stack didn't help me identify where the bug is but the but was a normal coding error. Not having an exception that included a line number of my code made this particularly difficult to debug/isolate.
The underlying problem was caused by the URL being used when the course was written no longer working. From site: https://file-examples.com/.

Related

Getting a discord bot to set the role of a guild member from DM's

So i am trying to make my discord bot add roles to someone that dm-ed it, and i cant seem to make it work
Here is the code:
guild.members.fetch(message.author)
.then(member => {
member.roles.set(['roleID']);
});
This is the error that i get:
(node:5692) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'set' of undefined
at D:\Documents\GitHub\InfinitBotPrototip\index.js:48:24
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:5692) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise w
hich was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhand
led_rejections_mode). (rejection id: 1)
(node:5692) [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 e
xit code.

How do I get the most recent message in discord.js?

Every time a message is sent in a specific channel, I want to print it to the console (with console.log). I am also going to color it with npm install colors. I go everywhere, even on Stack Overflow, but I cannot seem to find any information. I am coding a Scholastic Bowl-helping bot. Below is the code I have tried (I found this on Stack Overflow.)
message.fetch({ limit: 1 }).then(messages => {
let lastMessage = message.first();
if (message.channel.lastMessage = 'channel-id'){
console.log(lastMessage.red);
}
})
(Note that when I say 'channel-id' I mean the actual ID of the channel.)
The error I am getting is that message.first is not a thing.
How do I fix this error, and how can I get the most recent message in discord.js?
Edit: The exact error I got is this:
(node:12352) UnhandledPromiseRejectionWarning: TypeError: messages.first is not a function
at C:\Users\[user redacted]\Desktop\SchoBot\index.js:57:32
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:12352) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12352) [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.
Below is the edit for the 3rd comment on this question (sorted by oldest):
message.channel.fetch({ limit: 1 }).then(messages => {
let lastMessage = message.channel.first();
if (message.channel.lastMessage = 'channel-id'){
console.log(lastMessage.red);
}
})
Use message.channel.messages.fetch() instead of message.channel.fetch().
I didn't find the message.first function in the discord.js documentation, so I am not sure if it works. But you don't really need that function to fetch a message. The fetch function already did that for you.
In your case, the option limit: 1 will only return the most recent message, which is the command you use to trigger the fetch. If you want to fetch the most recent message but not your command, you should use limit: 2 instead and remove your command in the object later.
The fetch function will return an object containing message id and the content.
I assume that message.fetch needs to be message.channel.fetch

NodeJs,Canvas UnhandledPromiseRejectionWarning: Error: Unsupported image type

Dependency :
canvas > version: 2.6.1
node > version: 12.12.62
I am trying to do bellow :
const { createCanvas, loadImage } = require('canvas');
var image = await loadImage("png.png");
But, the same piece of code is perfectly working for *.jpeg file type.
Checked all possible points still having bellow error :
(node:2536) UnhandledPromiseRejectionWarning: Error: Unsupported image type
at setSource (********\node_modules\canvas\lib\image.js:91:13)
at Image.set (********\node_modules\canvas\lib\image.js:62:9)
at ********\landmark-detection\node_modules\canvas\index.js:34:15
at new Promise (<anonymous>)
at loadImage (********\node_modules\canvas\index.js:23:10)
at imageReconstruction (********\landmark-detection\app.js:159:22)
at ********\app.js:102:36
(node:2536) 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: 2)
(node:2536) [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.

UnhandledPromiseRejectionWarning even though using .catch() (Node.js)

I'm using Node.js and Express to build a REST api. The database operations are in a separate file called "db.js". Here's the gist of the code:
...
const db = require('./db');
app.get('/b50api/workitems/me/:pernr', function (req, rs) {
db.getWorkItems("200805").then((items, output)=>{
console.log("ITEMS:", items);
console.log("OUTPUT:", output);
}).catch(err => {
console.log("ERROR:", err);
})
})
...
When I hit this endpoint the program fails with the error below.
I created a test .js file (below) and this runs just fine:
const db = require('./db');
function test() {
db.getWorkItems("200805").then((items, output)=>{
console.log("ITEMS:", items);
console.log("OUTPUT:", output);
}).catch(err => {
console.log("ERROR:", err);
})
}
test();
What am I missing??
Here's the error information when running in my REST api:
(node:14612) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(node:14612) UnhandledPromiseRejectionWarning: TypeError: p.callNotify is not a function
at C:\inetpub\wwwroot\PersonnelApps\b50api\node_modules\msnodesqlv8\lib\procedure.js:332:11
at C:\inetpub\wwwroot\PersonnelApps\b50api\node_modules\msnodesqlv8\lib\procedure.js:305:13
at C:\inetpub\wwwroot\PersonnelApps\b50api\node_modules\msnodesqlv8\lib\procedure.js:194:9
at runNextTicks (internal/process/task_queues.js:62:5)
at processImmediate (internal/timers.js:429:9)
(node:14612) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14612) [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.
Turns out the issue was it was attempting to connect to the database as some built-in account. I need to change the site to an application and change the account it's running under to the one that needs to authenticate to the SQL Server (along with making that same account one that can log on as a service).

How I can create a file using multiple .chunk files?

I have more .chunk files which I fetch using node-fetch and using these files I want to create another file by "gathering them together" somehow.
I tried with this:
var fileStream = await fs.createWriteStream(`./paks/${file.Filename.slice(26)}`)
//below code is in a loop, above isn't in a loop
await fetch(chunkURL)
.then(async res => {
if(chunks.indexOf(chunk) == chunks.length - 1) await res.body.pipe(fileStream, { end: true })
else await res.body.pipe(fileStream, { end: false })
})
but the file is corrupted after it finishes, I also tried with flieStream.write(res.body, { end: true }) instead of res.body.pipe(fileStream, { end: true }) but I get the following error:
(node:12110) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object
at validChunk (_stream_writable.js:265:10)
at WriteStream.Writable.write (_stream_writable.js:300:21)
at /root/api/getUpdate.js:97:87
at processTicksAndRejections (internal/process/task_queues.js:85:5)
at async /root/api/getUpdate.js:94:57
(node:12110) 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: 2)
(node:12110) [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.

Categories

Resources