isage / lua-resty-moongoo

MongoDB library for OpenResty
Do What The F*ck You Want To Public License
118 stars 33 forks source link

How to extract _id #10

Closed mliker closed 7 years ago

mliker commented 7 years ago

I looked at you lua-cbson project, but still can't figure it out.

local doc, err = users:find_one({ email = "user@domain.com" })
for k,v in pairs(doc) do
  ngx.say(k,v)
end

results in: bad argument #2 to 'say' (string, number, boolean, nil, ngx.null, or array table expected, but got userdata)

isage commented 7 years ago

Sadly, openresty's ngx.say doesn't automatically call __tostring metamethod on userdata, as opposed to lua's print. So you'll need to explicitly call tostring yourself, e.g.:

local doc, err = users:find_one({ email = "user@domain.com" })
for k,v in pairs(doc) do
  ngx.say(tostring(k), tostring(v))
end
mliker commented 7 years ago

Excellent! Thank you for your answer.