在 wasm 构建中使用可选参数

huangapple go评论79阅读模式
英文:

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"})

huangapple
  • 本文由 发表于 2021年11月16日 05:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/69981089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定