Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Json Encode problems while using Google Firebase, is there any solutions?

Asked by 7 years ago

Today i just started using Google Firebase, after finding out it could help my problem with quick data transfer between servers. I am trying to use Google Firebase to get data quickly from server to server, but ran into this problem when i tried using the module script for this. Apparently trying to call one of the functions to give data always led to an error.

I would very much appreciate it if someone with experience with Firebase could give their advice on how to solve this problem. And underneath this is both the script and module script i used.

Here is the Script that uses the module script:

local Data = require(script.Firebase)
local Url = "https://project-siege.firebaseio.com/"


Data.Init(Url)

Data:Patch("users/4402987",{level = 9000, name = "astrallife"})

And here is the Module script itself: https://www.roblox.com/catalog/297221345/Rukiryos-Firebase-Module

I will still show the rest of the script here, for your convenience.

local module = {}

--Written by Rukiryo

--------------------------------------------------
--------------------EXAMPLES----------------------
--------------------------------------------------
--[[

    Basic Initialization
        local data = require(game.Workspace.Firebase)
        local url = 'https://your-firebase-url.firebaseio.com/'
        data.Init(url)
        data.Authenticate("my secret token") -- Optional but recommended

    Adding data for somebody
        module.Put("users/4402987",{level = 5, age = 19, name = "Rukiryo"})

    Deleting somebody's data
        module.Delete("users/4402987")

    Getting someone's data
        module.Get("users/4402987")

    You can put to any directory you want, even those that don't exist!

        module.Put("newsubtable/tableitem",{value = 5})
]]
--------------------------------------------------
----------------------API-------------------------
--------------------------------------------------
--[[
INIT
void Init (string url)
--Give the module a string to append before each request. If you wish to manually enter the url each time, do not call Init.

AUTHENTICATE
void Authenticate(string key)
--Appends a key to the end of requests. The key should be your security token. Read the Security section below for more.

POST
table Post(string path, table data, string header = "")
--Posts data. Returns data written (for verification). Post overwrites any info.
Returns name of data put: { "name": "-INOQPH-aV_psbk3ZXEX" } If you do not wish to receive a weird string, use PUT.

PUT
table Put(string path, table data)
--Puts data. Data will be overwritten at the path specified. All names will be set as you specify, no weird string.
Returns table containing information put (for verification)

GET
table Get(string path)
--Gets data. Returns requested data.

PATCH
table Patch(string path, table data)
--Patches data. Lets you update children without erasing content you do not mention. Creates new children that didn't exist previously.

DELETE
void Delete(string path)
--Deletes the specified path. 


]]
--------------------------------------------------
------------------Security------------------------
--------------------------------------------------
--[[
For help setting up security, visit https://www.firebase.com/docs/security/quickstart.html
If your wish to use my module's built in security, I've written a basic passworded protection code snippet.
This snippet will protect your entire database under a "Secret" token. This is available in your database.
This security rule will allow your data to be posted to / read only with an authentication key.

[code starts here]

{
    "rules": {
        ".read": "auth != null && auth == 'YOUR SECRET TOKEN HERE'",
        ".write": "auth != null && auth == 'YOUR SECRET TOKEN HERE'"
    }
}

[code ends here. do not paste these tags]

]]
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------

module.url = ""
module.http = nil
module.auth = ""

module.Init = function(u)
    module.url = u
    module.http = game:GetService("HttpService")
end

module.Authenticate = function(key)
    module.auth = key
end

module.Post = function(path, data, header)
    if header == nil then header = "" end
    local jData = module.http:JSONEncode(data)
    local finalPath = module.AuthKey(module.url..path..".json"..header)
    --print(finalPath)
    return module.http:PostAsync(finalPath, jData)
end

module.Get = function(path)
    local sendData = module.AuthKey(module.url..path..".json")
    --print(sendData)
    local gottenData = module.http:GetAsync(sendData)
    --print(gottenData)
    if gottenData ~= "null" then
        return module.http:JSONDecode(gottenData)
    end
    return nil
end

module.Put = function(path, data)
    return module.Post(path, data, "?x-http-method-override=PUT") 
end

module.Patch = function(path, data)
    return module.Post(path, data, "?x-http-method-override=PATCH")
end

module.Delete = function(path)
    return module.http:PostAsync(module.AuthKey(module.url..path..".json?x-http-method-override=DELETE"), module.http:JSONEncode({}))
end

module.AuthKey = function(str)
    if module.auth == "" then return str end
    if str.find(str, "?") ~= nil then
        return str.."&auth="..module.auth
    else
        return str.."?auth="..module.auth
    end
end


return module

Answer this question