Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

JSInterop returns function not defined

$
0
0

I am trying to call JS functions from C# using JS Interop. I have defined two methods and then calling using InvokeVoidAsync.

The problem is in browsers like edge and firefox the there comes error stating that function not defined (undefined) but in chrome it is fine and working whereas sometimes in chrome one of the function is returns error undefined.

window.getAccount = async function () {    try {        if (provider === null) {            return { "Success": false, "Address": null, "Message": "Metamask not detected" };        }        else {            const accounts = await provider.request({ method: "eth_requestAccounts" });            console.log("connected accounts = ", accounts);            if (accounts.length !== 0 || accounts !== undefined) {                return { "Success": true, "Message": null, "Address": accounts[0].toString() };            }            else                return "new error";        }    } catch (err) {            if (err.code === 4001) {                /*  console.log("Please connect to MetaMask.");*/                return { "Success": false, "Address": null, "Message": "Allow to connect to Metamask" };  // Return a message indicating to connect to MetaMask            } else {                console.error(err);                console.log(err);                //throw err; // Re-throw the error                return { "Success": false, "Address": null, "Message": err.toString() };            }    }}

and second function

window.getChainId = async function () {    return chainId.toString();}

I am calling these function in C# like -

ConnectWallet connectResponse = await JsRuntime.InvokeAsync<ConnectWallet>("getAccount");

and

string chainId = await JsRuntime.InvokeAsync<string>("getChainId");

any suggestions how can do this right?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>