我正在嘗試將以下代碼從 IndexedDB JavaScript API 轉換為 GO WASM:
request.onupgradeneeded = function(event) {
var result = event.target.result;
var objectStore = result.createObjectStore("employee", {keyPath: "id"});
objectStore.add({ id: "00-01", name: "Karam", age: 19, email: "[email protected]" });
}
所以,我寫道:
var dbUpgrade js.Func
var result, request js.Value
dbUpgrade = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer dbUpgrade.Release()
result = this.Get("result")
var objectStore = result.Call("createObjectStore", "employee")
objectStore.Call("add", `{ id: "00-01", name: "Karam", age: 19, email: "[email protected]" }`)
window.Call("alert", "First record posted.")
return nil
})
request.Set("onupgradeneeded", dbUpgrade)
但我收到以下運行時錯誤:
panic: JavaScript error: Failed to execute 'add' on 'IDBObjectStore': The object store uses out-of-line keys and has no key generator and the key parameter was not provided.
我明白原因是因為不包括{keyPath: "id"}atresult.Call("createObjectStore", "employee"來匹配 JavaScript 的,但我嘗試了下面的方法,但沒有任何效果:
// 1.
result.Call("createObjectStore", "employee", "{keyPath: `id`}")
// 2.
key, _ := json.Marshal(map[string]string{"keyPath": "id"})
result.Call("createObjectStore", "employee", key)
// 3.
result.Call("createObjectStore", `"employee", "{keyPath: "id"}"`)
有沒有想過如何制作?
uj5u.com熱心網友回復:
基于以下檔案ValueOf:
https://pkg.go.dev/syscall/[email protected]#ValueOf
這應該有效:
result.Call(...,map[string]interface{}{"keyPath": "id"})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358276.html
標籤:走
