How to pass a value rather than object ?

main.js (sends a msg to renderer.js)

    console.log("2  main.js 'did-start-navigation' =  " + url) **// this shows a value**
 win.webContents.send('Send-url', url) ;   
}) // 'did-start-navigation' ```


preload.js  (receives message from main.js , them preload.js plugs message into id="url")
```ipcRenderer.on('Send-url', holdUrl => {
  document.getElementById("url").value = holdUrl;
  console.log('2 holdUrl = ' +  holdUrl)  **// this shows a value**
}) ```

index.html
``` <input id="url" type="url" value="https://google.com"/> ```

But what gets plugged into <input> is this:   '[object Object]' 

How do I pass a value , not an object ?

Thanks

At [quote=“vmars316, post:1, topic:650351”]
ipcRenderer.on(
[/quote] you are calling the arrow function. This arrow function is returning something which you are assigning to the “url” element. If this “holdUrl” is an object, then you should rather assing the “text” or “html” property instead. What is it returning in “holdUrl?”

I just thought that “holdUrl =>” might have to be written as “(holdUrl) =>”. I have ccome up against weird bugs related to arrow functions in the past. Try it and see if it works.

Thank you for valuable information

Thanks radmo ,
Sorry to say that didn’t work .
This console.log:
main.js
view.webContents.on(‘will-navigate’, (event, url) => {
console.log(" 1 main.js view.webContents.on( will-navigate = " + url)
win.webContents.send(‘Send-url’, url) ;
})
shows url correctly.
But when url gets to:
preload.js
ipcRenderer.on(‘Send-url’, url => {
console.log('2 url = ’ + url)
document.getElementById(“url”).value = url ;
console.log('2 url = ’ + url)
})
it is an object , rather than the value of that object .
BTW: for simplicity , I dropped holdUrl .
Thanks for your help…
https://gist.github.com/58b504c9cd156eacb71dbfe4238a3066

TBH I don’t know anything about the framework your using but it seem your trying to mutate one of the function arguments then return it without return.

When you call a function you can store its value to a variable e.g.

const myFunc = function(){return "this is a return value"}
let runFunction = myFunc()

so the variable runFunction stores the return value of myFunc() after it has been run which is immediately

You can also do it another way with a global variable e.g.

var global = undefined
const myFunc = function(){ return global = 'another return value'}
myFunc()

So now global has been reassigned to the value of myFunc()

If its not a global variable you cant do this:

const func1 = function(value){ value = 6789}
const func2 = function(){
var randomVal = 12345
func1(randomVal);
console.log(randomVal) // logs out 12345 not 6789
}

You can however do this:

const func1 = function(value){ return value + 500000}
const func2 = function(){
var randomVal = 12345
randomVal = func1(randomVal);// reassign  randomVal to return of func1
console.log(randomVal) // logs out 512,345 
}

This might be what your wanting to do:

var holdURL = undefined; //GLOBAL
ipcRenderer.on('Send-url', function(){
    let value = document.getElementById("url").value; // get value of input or option element with id "url"
    return holdURL = value; // reassign GLOBAL variable holdURL
}

Steve , Thanks for your detailed Reply .
I dont want to return anything .
I am merely trying to update

with a new value , sent by , main.js
When it is sent to preload.js is a variable , like “http://vmars.us”
But when it is received by preload,js , it is now an [object Object] .
Why did it get changed to an object ?
What do I need to do to change it back to a variable ?
https://gist.github.com/58b504c9cd156eacb71dbfe4238a3066
Thanks

ah my bad… I don’t know how it got changed to an object but that is usually the case with events or if you want to return more than one thing from a function.

I’d rename the argument to event just for clarity…

Just console.log(event) the event and work out what property is the url.
Then either access the object or use destructuring in the arguments. e.g

ipcRenderer.on('Send-url', event => {
    console.log(event)// e.g {target: "someElement" , type: "click", url: "http://vmars.us"}
    let element = document.getElementById("url");
    element.value = event.url // access event object and update url input
})

or use object destructuring

// {url} = I only want the url property of the event object (first argument)
// {url = "fallback.com"} = if(event.url == undefined) event.url = 'fallback.com'
ipcRenderer.on('Send-url', ({url = 'fallback.com'}) => {
    let element = document.getElementById("url");
    element.value = url
})

Thanks for your help…

1 Like

Thank you for sharing this useful knowledge.

1 Like

Thank you sharing!