Nakama Error when trying to modify existing builtin api

83 Views Asked by At

Hi i wanted to add metadata in the builtin api "Update Account" in nakama , however whenever i try open in nakama console it w=showing me error "rpc error: code = Internal desc = Could not run runtime Before function." i dont know why. cause im new in nakama

kindly help .

function InitModule(
    ctx: nkruntime.Context,
    logger: nkruntime.Logger,
    nk: nkruntime.Nakama,
    initializer: nkruntime.Initializer
) {
    // initializer.registerBeforeUpdateAccount(beforeUpdateAccount);
    let beforeUpdateAccount: nkruntime.BeforeHookFunction<nkruntime.UserUpdateAccount> = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, data: nkruntime.UserUpdateAccount): nkruntime.UserUpdateAccount {
        // Check the group name does not contain profanity (containsProfanity implementation not provided)
        logger.info("beforeUpdateAccount rpc called.");

        let user_id = ctx.userId;
        try {
            const account = nk.accountGetId(user_id);
            let metadata = account.user.metadata || {};

            metadata.gameResult = "won";

            // Do not call nk.accountUpdateId here, as it's not necessary.

            // Return the updated metadata directly as 'nkruntime.UserUpdateAccount'
            const responseData: nkruntime.UserUpdateAccount = {
                userId: user_id,
                username: data.username, // Keep the original 'username' value from the 'data' argument
                displayName: data.displayName, // Keep the original 'displayName' value from the 'data' argument
                timezone: data.timezone, // Keep the original 'timezone' value from the 'data' argument
                location: data.location, // Keep the original 'location' value from the 'data' argument
                langTag: data.langTag, // Keep the original 'langTag' value from the 'data' argument
                avatarUrl: data.avatarUrl, // Keep the original 'avatarUrl' value from the 'data' argument
                metadata: metadata, // Return the updated 'metadata'
            };

            return responseData;
        } catch (error) {
            // If there's an error, you can return null or an empty object as appropriate.
            return { userId: user_id, username: "", displayName: "", timezone: "", location: "", langTag: "", avatarUrl: "", metadata: {} };
        }
    };

    initializer.registerBeforeUpdateAccount(beforeUpdateAccount);
    initializer.registerRpc("updateaccount", rpcCreateNewAccount);


    logger.info("JavaScript logic loaded.");
}

0

There are 0 best solutions below