检查 js.Value 是否存在。

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

Check if js.Value exists

问题

在JavaScript代码中,我有以下内容:

  1. function readAll() {
  2. var objectStore = db.transaction("employee").objectStore("employee");
  3. objectStore.openCursor().onsuccess = function(event) {
  4. var cursor = event.target.result;
  5. if (cursor) {
  6. alert("Name for id " + cursor.key + ", Value: " + cursor.value);
  7. cursor.continue();
  8. } else {
  9. alert("No more entries!");
  10. }
  11. };
  12. }

在我的等效GO代码中,我写了以下内容:

  1. var ReadAll js.Func
  2. ReadAll = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
  3. defer ReadAll.Release()
  4. cursor := this.Get("result")
  5. if cursor {
  6. _ = cursor.Get("key")
  7. value := cursor.Get("value")
  8. Window.Call("alert", value)
  9. cursor.Call("continue")
  10. } else {
  11. Window.Call("alert", "No more records")
  12. }
  13. return nil
  14. })
  15. db.ObjectStore.Call("openCursor").Set("onsuccess", ReadAll)

但是在编译时,我得到了以下错误:

  1. non-bool cursor (type js.Value) used as if condition

我该如何检查相关的js.value是否存在?

英文:

In the JavaScript code, I've:

  1. function readAll() {
  2. var objectStore = db.transaction("employee").objectStore("employee");
  3. objectStore.openCursor().onsuccess = function(event) {
  4. var cursor = event.target.result;
  5. if (cursor) {
  6. alert("Name for id " + cursor.key + ", Value: " + cursor.value);
  7. cursor.continue();
  8. } else {
  9. alert("No more entries!");
  10. }
  11. };
  12. }

In my equivalent GO code, I wrote:

  1. var ReadAll js.Func
  2. ReadAll = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
  3. defer ReadAll.Release()
  4. cursor := this.Get("result")
  5. if cursor {
  6. _ = cursor.Get("key")
  7. value := cursor.Get("value")
  8. Window.Call("alert", value)
  9. cursor.Call("continue")
  10. } else {
  11. Window.Call("alert", "No more records")
  12. }
  13. return nil
  14. })
  15. db.ObjectStore.Call("openCursor").Set("onsuccess", ReadAll)

But while compiling, I got:

  1. non-bool cursor (type js.Value) used as if condition

Ho can I check if the related js.value exists or no?

答案1

得分: 1

javascript中的if(cursor)是检查值是否为truthy

你的代码this.Get返回一个js.Value。它不是布尔值,不能在Go的if语句中使用。

syscall/js包中有一个Truthy()函数,你可以使用它来模拟javascript的truthy检查。

https://pkg.go.dev/syscall/js#Value.Truthy

  1. if cursor.Truthy() {
  2. ...
  3. }

要检查一个值是否为falsey,你可以对结果取反:

  1. if !cursor.Truthy() {
  2. ...
  3. }
英文:

The javascript if(cursor) is checking if the value is truthy.

Your code this.Get result in a js.Value. It's not boolean, and you can't use it in a Go if clause.

The syscall/js package has Truthy() that you can use to mimick javascript truthy check.

https://pkg.go.dev/syscall/js#Value.Truthy

  1. if cursor.Truthy() {
  2. ...
  3. }

And to check if a value is falsey, you negate the result:

  1. if !cursor.Truthy() {
  2. ...
  3. }

huangapple
  • 本文由 发表于 2021年11月17日 01:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/69993721.html
匿名

发表评论

匿名网友

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

确定