>
> > 您好,我按照你的办法,确实第一次加载成功, 但是第二次请求,仍旧出现了之前的报错。
> >> 以下是我的配置:
>
> >> location ~ /test3/ {
>
> >> set $realip "61.135.251.86";
> >> content_by_lua '
>
> >> local countryres = ngx.location.capture("/getipinfo/",
> >> {args={ip=ngx.var.realip}});
> >> local xml = require("xml");
> >> ngx.say(countryres.body);
> >> local root = xml.eval(countryres.body);
> >> local countrynode = root:find("country");
> >> local country = countrynode[1];
> >> ngx.say(country);
> >> ';
> >> }
>
> >> 第一次请求得到了:
>
> >> <?xml version="1.0" encoding="UTF-8"?>
> >> <ip><result>1</result><country>北京</country><city>北京市</city><sp>网通</
> >> sp></ip>
>
> >> 北京
>
> >> 第二次就报错了:
> >> 2012/09/11 16:59:47 [error] 8221#0: *449 lua handler aborted: runtime
> >> error: attempt to index a nil value
> >> stack traceback:
> >> [C]: in function 'eval'
>
> >> 求教。。
>
> > 看起来之前那个问题已经解决,至少不是报xml为nil,
> > 另外stack traceback:
> > [C]: in function 'eval'
> > 这个是完整的错误日志吗?
>
> >> On 9月11日, 下午12时50分, pengqi <
fengm...@gmail.com> wrote:
> >> > hi,
>
> >> > 现在的问题是,第一次访问,require
>
> >> "luaxml"的结果:require函数将package.loaded["luaxml"]设置为true,模块里面的module函数会定义一个全局变量xml(它的值为模块本身),另外将package.loaded["xml"]设置为模块本身(这个是我们需要的),此时访问xml变量是正常的;
>
> >> > 但是第二次访问时,require "luaxml" ,require函数会检查
>
> >> package.loaded["luaxml"]是否为空,不为空的话直接返回它(也就是返回true),不做任何事情了(当然也不会定义一个全局变量xml),而且lua模块现在不会继承上一次请求所新建的全局变量,也就是说此时xml实际上未定义。
>
> >> > 解决方法是将文件重新命名为xml.lua,并写为local xml =
>
> >> require("xml"),这样做的话,require函数返回package.loaded["xml"](这个是我们需要的模块),并保存在局部变量中
>
> >> > 在 2012年9月11日 下午12:37,Chunlin Zhang <
zhangchun...@gmail.com>写道:
>
> >> > > 嗯,直接用 ngx.print(xml); 我这里会报错,但是报的是
> >> > > 2012/09/11 12:34:41 [error] 7154#0: *20 lua handler aborted: runtime
> >> > > error: /usr/local/openresty/nginx/conf/lua/test.lua:7: bad argument #1
> >> > > to 'print' (non-array table found)
> >> > > 但是如果 ngx.print(type(xml)) 或者 ngx.print(prettytostring(xml)) 就没问题
>
> >> > > 2012/9/11 陈伟 <
realmikojor...@gmail.com>:
> >> > > > content_by_lua '
> >> > > > require("LuaXml");
> >> > > > local xml = xml;
> >> > > > local root = xml.new("root");
>
> >> > > > ngx.print(xml);
>
> >> > > > ';
> >> > > > }
>
> >> > > > 还是报错:[error] 4197#0: *255 lua handler aborted: runtime error:
> >> [string
> >> > > > "content_by_lua"]:4: attempt to index local 'xml' (a nil value)
>
> >> > > > On 9月11日, 上午10时36分, Chunlin Zhang <
zhangchun...@gmail.com> wrote:
> >> > > >> 你也可以在外面把lua代码调试一下,再放到openresty里跑
> >> > > >> require"LuaXml"以后是在全局变量里多了一个 xml 的表,要不你用
>
> >> > > >> require "LuaXml"
> >> > > >> local xml = xml
>
> >> > > >> 这样试试
>
> >> > > >> 2012/9/11 陈伟 <
realmikojor...@gmail.com>:
>
> >> > > >> > 再贴一下 LuaXml.lua 模块的部分源代码:
>
> >> > > >> > require("LuaXML_lib")
>
> >> > > >> > local base = _G
> >> > > >> > local xml = xml
> >> > > >> > module("xml")
>
> >> > > >> > -- symbolic name for tag index, this allows accessing the tag by
> >> > > >> > var[xml.TAG]
> >> > > >> > TAG = 0
>
> >> > > >> > 。。。。(下面都是函数)
>
> >> > > >> >
> >> > > >
> >> > >
> >> > --
> >> > Jinglong
> >> > Software Engineer
> >> > Server Platforms Team at Taobao
>
> >>
> > --
> > Jinglong
> > Software Engineer
> > Server Platforms Team at Taobao
>
> --
> Jinglong
> Software Engineer
> Server Platforms Team at Taobao