Blazor Javascript Interop Returns Null

681 views Asked by At

I have a Javascript function which should (does) return a string. The issue is that the C# part is always null although I've verified the JS function IS returning a string.

Here's the Javascript function:

window.get_current_user = () => {
        Moralis.User.currentAsync().then(function (user) {
            var wallet = user.get('ethAddress');
            return wallet;
        });
    }

Here's the C# calling it

 private async void GetAddress()
 {
    var userAddress = await _js.InvokeAsync<string>("get_current_user");
    _js.InvokeVoidAsync("alert", userAddress);
 }

I've used the Chrome Dev Console dev tools to breakpoint at the line in which it returns the value and verified the value is correct and a string. enter image description here

I've also placed a breakpoint on the 4th line of the GetAddress function and see that the value of 'userAddress' is indeed null

enter image description here

1

There are 1 answers

0
JS Interop On

Do the following, the return should be outside of the second function:

window.get_current_user = () => {
var wallet = null;
        Moralis.User.currentAsync().then(function (user) {
            wallet = user.get('ethAddress');
            
        });
return wallet;
    }