英文:
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 && 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()
/* Here I've an error */
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)
/* This line is wrong, can not access `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
}
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("this is: ", dbx)
js.Global().Call("alert", "ok.")
return nil
})
db.Set("onsuccess", dbOk)
To be equivalent to this JavaScript:
var dbx
request.onsuccess = function(event) {
dbx = request.result;
console.log("success: "+ dbx);
};
And this go code:
js.Global().Get(dbx).Call("transaction", "readwrite").Call("objectStore", "employee").Call("add", `{ id: "00-03", name: "Karam", age: 19, email: "kenny@planet.org" }`)
To replace this JavaScript code:
dbx.transaction(["employee"], "readwrite")
.objectStore("employee")
.add({ id: "00-03", name: "Karam", age: 19, email: "kenny@planet.org" });
答案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("result")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论