0

I'm using Blazor WASM with JavaScript for Web API. I have this error:

Error: Could not find 'AddDb' ('AddDb' was undefined).

JavaScript

export function AddDB() {
const DB_NAME = 'product';
const DB_VERSION = 1; // Use a long long for this value (don't use a float)
const DB_STORE_NAME = 'MHCproduct';

var db;

// Used to keep track of which view is displayed to avoid uselessly reloading it
var current_view_pub_key;

function openDb() {
    console.log("openDb ...");
    var req = indexedDB.open(DB_NAME, DB_VERSION);
  }
  }

Razor page

 <div class="row">
    <span class="cuscol-100" style="text-align:center;background-color:white">
        <button class="btn btn-primary" style="width:100%" @onclick="()=>StartDB(elementX)">Calculate</button>
    </span>
</div>

@code{
    string name = string.Empty;
    IJSObjectReference module;

    private ElementReference elementX;

    async Task StartDB(ElementReference element)
    {
        module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./js/JavaScript.js");
        await module.InvokeVoidAsync("AddDb", element);
        Console.WriteLine("Berjaya");

    }
}
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
Hani
  • 21
  • 1
  • 12

2 Answers2

2

Didn´t work with JSInterop for a longer time now, but is it possible that the error occurs because you enter a parameter even though the JS function does not need one? Can´t check it right now, but maybe change await module.InvokeVoidAsync("AddDb", element); to await module.InvokeVoidAsync("AddDb");.

Other possible solution: Did you add a <script src="xyz.js"></script> to your index.html file?

devbf
  • 412
  • 5
  • 15
1

Just stumble on similar issue. Not sure what resolve it, but I do this to resolve the issue:

On your Solution Explorer in Visual Studio: Right click the solution and choose clean solution.

Similar problem from this link: Blazor Component Javascript Errors (Microsoft.JSInterop.JSException)