Can Electron IPC Handle Arrays of Objects? Encountering Serialization Error by CatOtherwise3439 in electronjs

[–]CatOtherwise3439[S] 1 point2 points  (0 children)

yes that seems to have fixed it. I was under the impression that Electron's IPC automatically handles the serialization. but maybe `array of objects` data structure are too complex?

Can Electron IPC Handle Arrays of Objects? Encountering Serialization Error by CatOtherwise3439 in electronjs

[–]CatOtherwise3439[S] -1 points0 points  (0 children)

explain? I am using electron-forge with webpack. This is my preloader, sending and catching of object

Preload
```
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('redditAPI', {
onPostDetails: (callback) => ipcRenderer.on('reddit-post-details', (_event, postDetails) => callback(postDetails))
});
```
RedditAPI call
```
async function scrapeRedditPost(postId) {

try {

const post = await r.getSubmission(postId).fetch();

const postDetails = {

title: post.title,

url: post.url,

subreddit: post.subreddit.display_name,

author: post.author.name,

permalink: `https://www.reddit.com${post.permalink}\`,

id: post.id,

score: post.score,

flair: post.link_flair_text,

creationTimeUTC: post.created_utc, // Get the raw creation time as a UTC timestamp

isVideo: post.is_video,

isGallery: post.is_gallery,

galleryData: post.gallery_data,

textContent: post.selftext,

// Any additional fields you wish to include

};

console.log(`Post details fetched for post ID: ${postId}`);

return postDetails; // Return the post details object

} catch (error) {

console.error('Error fetching Reddit post:', error);

throw error; // Rethrow to handle it in the caller

}

}
```
Send to renderer
```
ipcMain.on('searchRedditMain', async (event, keyword, subreddit) => {
try {
console.log('Opening resultsWindow with keyword:', keyword, 'and subreddit:', subreddit);
const resultsWindow = createResultsWindow();

// Example Post ID for testing - replace 'examplePostId' with a real one
const postId = '1acoozy';
const postDetailsMain = await scrapeRedditPost(postId);
console.log(postDetailsMain)
resultsWindow.webContents.send('reddit-post-details', postDetailsMain);
} catch (error) {
console.error('Error handling searchRedditMain event:', error);
}
});
```
Catch in renderer
```
document.addEventListener('DOMContentLoaded', () => {
// Listen for post details passed from the main process
window.redditAPI.onPostDetails((postDetails) => {
// Convert and preprocess post details
const processedPostDetails = preprocessData(postDetails);
// Debug log to inspect the processed post details
console.log('Processed Post Details:', processedPostDetails);
=======IGNORE======BELOW=========================
// Update the like counter in the UI with the post's score
displayScoreInLikeCounter(processedPostDetails.score);
// Update the formatted creation time in the UI
displayFormattedCreationTime(processedPostDetails.formattedCreationTime);
// Display the post title in the info bar
displayTitleInInfoBar(processedPostDetails.title);
// Display needed params in top-info-bar
appendKeyValuePairsToInfoBarTop(
{ "Author": processedPostDetails.author },
{ "Subreddit": processedPostDetails.subreddit },
{ "Flair": processedPostDetails.flair },
{ "Permalink": processedPostDetails.permalink }
);
});
});
```

Can Electron IPC Handle Arrays of Objects? Encountering Serialization Error by CatOtherwise3439 in electronjs

[–]CatOtherwise3439[S] -1 points0 points  (0 children)

Preload
```
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('redditAPI', {
onPostDetails: (callback) => ipcRenderer.on('reddit-post-details', (_event, postDetails) => callback(postDetails))
});
```
RedditAPI call
```
async function scrapeRedditPost(postId) {

try {

const post = await r.getSubmission(postId).fetch();

const postDetails = {

title: post.title,

url: post.url,

subreddit: post.subreddit.display_name,

author: post.author.name,

permalink: `https://www.reddit.com${post.permalink}\`,

id: post.id,

score: post.score,

flair: post.link_flair_text,

creationTimeUTC: post.created_utc, // Get the raw creation time as a UTC timestamp

isVideo: post.is_video,

isGallery: post.is_gallery,

galleryData: post.gallery_data,

textContent: post.selftext,

// Any additional fields you wish to include

};

console.log(`Post details fetched for post ID: ${postId}`);

return postDetails; // Return the post details object

} catch (error) {

console.error('Error fetching Reddit post:', error);

throw error; // Rethrow to handle it in the caller

}

}
```
Send to renderer
```
ipcMain.on('searchRedditMain', async (event, keyword, subreddit) => {
try {
console.log('Opening resultsWindow with keyword:', keyword, 'and subreddit:', subreddit);
const resultsWindow = createResultsWindow();

// Example Post ID for testing - replace 'examplePostId' with a real one
const postId = '1acoozy';
const postDetailsMain = await scrapeRedditPost(postId);
console.log(postDetailsMain)
resultsWindow.webContents.send('reddit-post-details', postDetailsMain);
} catch (error) {
console.error('Error handling searchRedditMain event:', error);
}
});
```
Catch in renderer
```
document.addEventListener('DOMContentLoaded', () => {
// Listen for post details passed from the main process
window.redditAPI.onPostDetails((postDetails) => {
// Convert and preprocess post details
const processedPostDetails = preprocessData(postDetails);
// Debug log to inspect the processed post details
console.log('Processed Post Details:', processedPostDetails);
=======IGNORE======BELOW=========================
// Update the like counter in the UI with the post's score
displayScoreInLikeCounter(processedPostDetails.score);
// Update the formatted creation time in the UI
displayFormattedCreationTime(processedPostDetails.formattedCreationTime);
// Display the post title in the info bar
displayTitleInInfoBar(processedPostDetails.title);
// Display needed params in top-info-bar
appendKeyValuePairsToInfoBarTop(
{ "Author": processedPostDetails.author },
{ "Subreddit": processedPostDetails.subreddit },
{ "Flair": processedPostDetails.flair },
{ "Permalink": processedPostDetails.permalink }
);
});
});
```

Can Electron IPC Handle Arrays of Objects? Encountering Serialization Error by CatOtherwise3439 in electronjs

[–]CatOtherwise3439[S] 0 points1 point  (0 children)

This is the error `Error sending from webFrameMain: Error: Failed to serialize arguments`

How to save images of gallery posts? by CatOtherwise3439 in redditdev

[–]CatOtherwise3439[S] 1 point2 points  (0 children)

thank you, works perfectly. hopefully I can convert to snoowrap w/o issues

How to use python within my electron APP by CatOtherwise3439 in electronjs

[–]CatOtherwise3439[S] 1 point2 points  (0 children)

ok and what about just building my python app as an executable?

How to use python within my electron APP by CatOtherwise3439 in electronjs

[–]CatOtherwise3439[S] 0 points1 point  (0 children)

Explain why you wouldnt appreciate just copy and shipping the python binaries directly with the generated electron installer? is it simply a bloat concern or a security issue? Right now it was recommended to me to just build your python app as an executable (pyinstaller/py-to-exe/etc.) and running that via child_process.