英文:
Using optional parameters in wasm building
问题
我正在尝试将以下代码从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: "kenny@planet.org" });
}
所以,我写了以下代码:
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: "kenny@planet.org" }`)
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.
我理解的原因是因为在result.Call("createObjectStore", "employee")
中没有包含{keyPath: "id"}
以匹配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"}"`)
有什么想法如何解决这个问题吗?
英文:
I'm trying to convert the below code from IndexedDB JavaScript API into 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: "kenny@planet.org" });
}
So, I wrote:
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: "kenny@planet.org" }`)
window.Call("alert", "First record posted.")
return nil
})
request.Set("onupgradeneeded", dbUpgrade)
But i got the below runtime error:
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.
I understand the reason is because of not including {keyPath: "id"}
at result.Call("createObjectStore", "employee"
to match the JavaScript one, but I tried the below and nothing worked:
// 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"}"`)
Any thought how to make it?
答案1
得分: 1
根据ValueOf
的文档:
https://pkg.go.dev/syscall/js@go1.17.3#ValueOf
这应该可以工作:
result.Call(...,map[string]interface{}{"keyPath": "id"})
英文:
Based on the documentation for ValueOf
:
https://pkg.go.dev/syscall/js@go1.17.3#ValueOf
This should work:
result.Call(...,map[string]interface{}{"keyPath": "id"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论