JSON parse error in RPC call

Hi,

I have this error in Firefox when trying to call an RPC function frome the JS client:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

The client code:
const response = await client.rpc(session, "create_match_rpc", {})

The RPC function:

type response struct {
	MatchID string   `json:"matchID"`
}

func createMatchRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
	params := make(map[string]interface{})
	if err := json.Unmarshal([]byte(payload), &params); err != nil {
			return "", err
	}

	matchID, err := nk.MatchCreate(ctx, "MatchHandler", params)

	if err != nil {
			return "", err
	}

	r := response{matchID}
	rr, err := json.Marshal(r)
	logger.Printf("rr:")
	logger.Printf(string(rr))
	return string(rr), nil
}

In Chrome everything works fine, the response’s payload looks like this: {"matchID":"caf156d8-55c6-47fe-89c2-acbeb9c7c77d.nakama1"}

But in Firefox, when looking at firebug’s network tab, I can see that the response’s payload looks like this: H4sIAAAAAAAA/6pWKkiszMlPTFGyUqqOUcpNLEnO8HSJUbKKUUpLTDFPtDQ20DU0NzXVNTE2NtBNMjY10jUwsjC0SDYysTQwM9bLS8xOzE00jFGqVaoFBAAA//8kAbCZTAAAAA==

I’m using Nakama JS client version 2.0.1

I believe you need to wrap the result in nk.json_encode().

Here is what our client code looks like in lua (stripped of entry fees and other details).

You might want to re-check the go example module in the docs as well.


local function create_match(context, payload)
    local modulename = "match_handler"
    local setupstate = {initialstate = nk.json_decode(payload)}

    local matchid = nk.match_create(modulename, setupstate)

    return nk.json_encode({["match_id"] = matchid, ["setupstate"] = setupstate})
end
nk.register_rpc(create_match, "create_match_rpc")

Also make sure you grab the latest build of the Nakama JavaScript client directly from the dist folder here https://github.com/heroiclabs/nakama-js/tree/master/dist - it’s newer than the tagged 2.0.1 release and contains a lot of fixes and updates.

We’ll tag a new official version of the client very soon, with even more improvements included.

Hi, thanks for your answers

@GroovyAntoidDev The Go example in the doc is the following:

func CreateMatchRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
    params := make(map[string]interface{})
    if err := json.Unmarshal([]byte(payload), params); err != nil {
        return "", err
    }

    modulename := "pingpong" // Name with which match handler was registered in InitModule, see example above.
    if matchId, err := nk.MatchCreate(ctx, modulename, params); err != nil {
        return "", err
    } else {
        return matchId, nil
    }
}

(source: https://heroiclabs.com/docs/gameplay-multiplayer-server-multiplayer/#manually )

As you can see, it returns the matchId, not a JSON formatted string.

In my code I use jsonResponse, err := json.Marshal(response) which is I believe the equivalent of local jsonResponse = nk.json_encode(response).

@zyro I’m already using the latest Nakama JS client, I redownloaded nakama-js.umd.js yesterday from this folder.

From what I’ve tested, it happens here: https://github.com/heroiclabs/nakama-js/blob/master/src/api.gen.ts#L572
But only in Firefox, because here the response body is not a JSON.