当我对从文件读取的字符串使用String.contains方法时,我的opa应用程序失败:
import stdlib.io.file
import stdlib.core
function start()
{
txt = string_of_binary(File.content("data.txt"))
jlog(txt)
b = String.contains(txt, "Rabbit")
<>Hello Bug</>
}
Server.start(
{port:8092, netmask:0.0.0.0, encryption: {no_encryption}, name:"bug"},
[ {resources: @static_resource_directory("resources")},
{register: {css: []} },
{page: start, title: "bug"},
]
)
我遇到以下错误:
bug serving on http://ks3098156.kimsufi.com:8092
[Opa] Server dispatch Decoded URL to /
STDERR:rabbit
/home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:26
_error = true;global.console.log("Uncaught exception : " + global.e.toString()
^
TypeError: Cannot call method 'toString' of undefined
at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:26:222
at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:27:78
at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:22:128
at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:28:263
at dispatcher_cps (/home/kayhman/website/rstEditor/code/bug_depends/bslNet.nodejs:60:165)
at Server.<anonymous> (/home/kayhman/website/rstEditor/code/bug_depends/bslNet.nodejs:59:437)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1514:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1410:22)
数据文件只包含一行:
rabbit
我的代码出了什么问题?
由于
答案 0 :(得分:1)
确实File.content中存在一个错误,它将在下一个版本中修复。
您可以使用首选(和工作)安全File.content_opt
函数:
function start() {
match(File.content_opt("data.txt")){
case {none} :<>No file</>
case {some:content} :
txt = string_of_binary(content)
jlog(txt)
b = String.contains(txt, "Rabbit")
<>Hello Bug</>
}
}