首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FiveM Lua: Fivem > Web > Fivem

FiveM Lua: Fivem > Web > Fivem
EN

Stack Overflow用户
提问于 2021-12-15 14:53:00
回答 1查看 769关注 0票数 1

在这个API上挣扎。它适用于邮递员期望的步骤:

用户完成聊天命令/user id。

2-脚本调用API并传递id。

3-使用该id的用户搜索数据库。

4- API传回该用户的用户名,并将其打印到聊天中。

我想这个代码是可怕的,完全错误的,但是我对Lua和FiveM代码非常陌生。这是错误代码。

代码语言:javascript
复制
FIVEM ERROR
[  script:TestingPOST] 2
[  script:TestingPOST] table: 000001ADC3990530
[  script:TestingPOST] 2
[  script:TestingPOST] nil
[  script:TestingPOST] SCRIPT ERROR: @TestingPOST/webhook_s.lua:16: attempt to index a nil value (local 'data')

我非常感谢你的帮助!)

我已经删除了API链接和auth令牌。

代码语言:javascript
复制
    RegisterCommand("user", function(source, args, rawCommand)
    
        local query = string.sub(rawCommand, 6)
    
        print(source)
        print(args)
        print(query)
    
        PerformHttpRequest('https://MYAPP.bubbleapps.io/version-test/api/1.1/wf/userdata', 
        function(err, text, header)
            local data = json.decode(text)
            TriggerClientEvent("chat:addMessage", source, {
                args {
                    string.format("Display name is %s", data.response.displayname )
                }
            })
    
            end, 'POST',
            json.encode({userid = query}), {["Authorization"] = 'TOKEN'})
    end)
EN

回答 1

Stack Overflow用户

发布于 2021-12-16 08:47:58

错误指示数组datanil,这意味着您在text中没有正确的值(如data = json.decode(text))

我对这个特定的API不太了解,但是作为一个一般的编程建议,您应该检查errtextheader的值,这些信息可能是代码不能工作的原因。如果你能在这里张贴这些值,那会有帮助

您还应该在代码中集成错误处理,以防API不可用或发生错误(例如,检查err == 200 )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70365760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档