1

Struggling with this API. It works on postman - expected steps:

1- User completes chat command /user [id].

2- Script calls the API and passes [id].

3- API search DB for users with that id.

4- API passes back the username of that user and prints it into the chat.

I imagine this code is horrific and totally wrong but I am very new to Lua and FiveM code. Here is the error code.

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')

I would really appreciate some help! :)

I have removed the API link and auth token

    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)

1 Answers1

0

The error indicates that the array data is nil, meaning you don't have a correct value in text (as data = json.decode(text))

I don't know much about this particular API but as a general programming advice, you should check the values of err, text and header, there's probably information as why the code isn't working. If you could post the values here that would help

You should also integrate error handling in your code in case the API is unavailable or an error occurs (by checking that err == 200 for instance)

Blaizz
  • 21
  • 1
  • 5