FiveM Lua: Fivem > Web > Fivem

使用这个 API 遇到了问题。在 Postman 中运行正常- 预期步骤:

1- 用户完成聊天命令 /user [id].

2- 脚本调用 API 并传递 [id].

3- API 在数据库中搜索具有该 ID 的用户.

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

我想象这段代码是可怕而且完全错误的,但我对 Lua 和 FiveM 代码非常陌生。以下是错误代码。

FIVEM 错误
[  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 链接和身份验证令牌

    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("显示名称为 %s", data.response.displayname )
                }
            })

            end, 'POST',
            json.encode({userid = query}), {["Authorization"] = 'TOKEN'})
    end)

原文链接 https://stackoverflow.com/questions/70365760

点赞
stackoverflow用户17023303
stackoverflow用户17023303

错误提示:此处错误表明数组 datanil,意味着 text 中没有正确的值(例如 data = json.decode(text))。

我对这特定的 API 不是很了解,但通常的编程建议是检查 errtextheader 的值,这些应该包含一些信息来说明代码运行不正常。如果您能在此处发布这些值,那会很有帮助。

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

2021-12-16 08:47:58