你可以将js.Value作为参数传递给js.Global().Get方法。

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

How can I use js.Value as argument to js.Global().Get

问题

尝试使用GO WebAssembly访问浏览器的IndexedDB,使用下面的代码,我能够创建indexedDB,但是在尝试使用js.Global().Get(dbx)访问它或者尝试访问this.result时出现错误。

//go:build js && wasm

package main

import (
	"fmt"
	"syscall/js"
)

var dbOk, dbErr js.Func
var db js.Value

func main() {
	fmt.Println("Hello WASM")
	db = js.Global().Get("indexedDB").Call("open", "Database", 1)

	dbOk = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		defer dbOk.Release()
/* 这里有一个错误 */
		dbx := this.result
		fmt.Println("this is: ", dbx)
/**********************/
		js.Global().Call("alert", "ok.")
		return nil
	})
	db.Set("onsuccess", dbOk)

	dbErr = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		defer dbErr.Release()

		js.Global().Call("alert", "sorry.")
		return nil
	})
	db.Set("onerror", dbErr)
	
    /* 这一行是错误的,不能访问`db` */
	js.Global().Get(dbx).Call("transaction", "readwrite").Call("objectStore", "employee").Call("add", `{ id: "00-01", name: "Karam", age: 19, email: "kenny@planet.org" }`)

	c := make(chan int)
	<-c
}

我得到的错误是:

# command-line-arguments
./wasm.go:21:14: this.result undefined (type js.Value has no field or method result)
./wasm.go:41:17: cannot use dbx (type js.Value) as type string in argument to js.Global().Get

为了将其映射到JavaScript,我希望这个go代码:

	dbOk = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		defer dbOk.Release()
		dbx := this.result
		fmt.Println("this is: ", dbx)
		js.Global().Call("alert", "ok.")
		return nil
	})
	db.Set("onsuccess", dbOk)

等价于这个JavaScript代码:

var dbx

request.onsuccess = function(event) {
    dbx = request.result;
    console.log("success: "+ dbx);
};

以及这段go代码:

js.Global().Get(dbx).Call("transaction", "readwrite").Call("objectStore", "employee").Call("add", `{ id: "00-03", name: "Karam", age: 19, email: "kenny@planet.org" }`)

替换这段JavaScript代码:

dbx.transaction(["employee"], "readwrite")
    .objectStore("employee")
    .add({ id: "00-03", name: "Karam", age: 19, email: "kenny@planet.org" });
英文:

Trying with GO WebAssembly accessing the browser IndexedDB, with the below code I was able to create the ibdexedDB, but I got an error while trying to access it using js.Global().Get(dbx) or even trying to access this.result

//go:build js &amp;&amp; wasm

package main

import (
	&quot;fmt&quot;
	&quot;syscall/js&quot;
)

var dbOk, dbErr js.Func
var db js.Value

func main() {
	fmt.Println(&quot;Hello WASM&quot;)
	db = js.Global().Get(&quot;indexedDB&quot;).Call(&quot;open&quot;, &quot;Database&quot;, 1)

	dbOk = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		defer dbOk.Release()
/* Here I&#39;ve an error */
		dbx := this.result
		fmt.Println(&quot;this is: &quot;, dbx)
/**********************/
		js.Global().Call(&quot;alert&quot;, &quot;ok.&quot;)
		return nil
	})
	db.Set(&quot;onsuccess&quot;, dbOk)

	dbErr = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		defer dbErr.Release()

		js.Global().Call(&quot;alert&quot;, &quot;sorry.&quot;)
		return nil
	})
	db.Set(&quot;onerror&quot;, dbErr)
	
    /* This line is wrong, can not access `db` */
	js.Global().Get(dbx).Call(&quot;transaction&quot;, &quot;readwrite&quot;).Call(&quot;objectStore&quot;, &quot;employee&quot;).Call(&quot;add&quot;, `{ id: &quot;00-01&quot;, name: &quot;Karam&quot;, age: 19, email: &quot;kenny@planet.org&quot; }`)

	c := make(chan int)
	&lt;-c
}

The error I got is:

# command-line-arguments
./wasm.go:21:14: this.result undefined (type js.Value has no field or method result)
./wasm.go:41:17: cannot use dbx (type js.Value) as type string in argument to js.Global().Get

To map it with JavaScript, I wanted this go:

	dbOk = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		defer dbOk.Release()
		dbx := this.result
		fmt.Println(&quot;this is: &quot;, dbx)
		js.Global().Call(&quot;alert&quot;, &quot;ok.&quot;)
		return nil
	})
	db.Set(&quot;onsuccess&quot;, dbOk)

To be equivalent to this JavaScript:

var dbx

request.onsuccess = function(event) {
    dbx = request.result;
    console.log(&quot;success: &quot;+ dbx);
};

And this go code:

js.Global().Get(dbx).Call(&quot;transaction&quot;, &quot;readwrite&quot;).Call(&quot;objectStore&quot;, &quot;employee&quot;).Call(&quot;add&quot;, `{ id: &quot;00-03&quot;, name: &quot;Karam&quot;, age: 19, email: &quot;kenny@planet.org&quot; }`)

To replace this JavaScript code:

dbx.transaction([&quot;employee&quot;], &quot;readwrite&quot;)
    .objectStore(&quot;employee&quot;)
    .add({ id: &quot;00-03&quot;, name: &quot;Karam&quot;, age: 19, email: &quot;kenny@planet.org&quot; });

答案1

得分: 2

在这个上下文中,this 是一个 JavaScript 对象,所以你需要获取它的属性 result

result := this.Get("result")

result 将是一个 js.Value,如果它是一个原始类型,你可以获取它的 Go 值,或者通过 result.Get() 进一步访问对象的元素,或者调用它的方法。

英文:

In this context, this is a Javascript object, so you have to get the property result of it:

result:=this.Get(&quot;result&quot;)

The result will be a js.Value, and you can get the the Go value of it if it is a primitive, or descend to further object elements using result.Get(), or Call a method of it.

huangapple
  • 本文由 发表于 2021年11月15日 22:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/69976232.html
匿名

发表评论

匿名网友

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

确定