“JavaScript 运行时函数无效。”,“key”:“afterUpdateAccount”

问题描述 投票:0回答:0

我尝试在用户更新后添加元数据。但是在 main.ts 中添加函数并转到 ApiExplorer UpdateAccount 后,它没有运行我的函数。在我的日志中显示此错误:

template_nk_backend | {“level”:“error”,“ts”:“2023-07-29T05:59:14.888Z”,“caller”:“server/runtime_javascript.go:477”,“msg”:“JavaScript运行时函数无效。 ”,“key”:“更新帐户后”}

不知道是什么导致了这个问题,因为目前对 nakama 来说是新的。请帮忙

这是我的main.ts:

 function InitModule(
    ctx: nkruntime.Context,
    logger: nkruntime.Logger,
    nk: nkruntime.Nakama,
    initializer: nkruntime.Initializer
  ) {
    let afterUpdateAccount: nkruntime.AfterHookFunction<
      void,
      nkruntime.UserUpdateAccount
    > = function (
      ctx: nkruntime.Context,
      logger: nkruntime.Logger,
      nk: nkruntime.Nakama
    ): nkruntime.UserUpdateAccount {
      // Check the group name does not contain profanity (containsProfanity implementation not provided)
      logger.info("afterUpdateAccount hook called.");
  
      let user_id = ctx.userId;
      try {
        const account = nk.accountGetId(user_id);
        let metadata = account.user.metadata || {};
  
        metadata.gameResult = "won";
  
        // Convert the metadata to a JSON string
        const metadataString = JSON.stringify(metadata);
  
        // Update the user's metadata using the JSON string
        nk.accountUpdateId(user_id, metadataString);
  
        // Return a dummy UserUpdateAccount object
        return {
          userId: user_id,
          username: "please",
          displayName: "please2",
          timezone: "",
          location: "",
          langTag: "",
          avatarUrl: "please3",
          metadata: { metadataString },
        };
      } catch (error) {
        // Handle the error as appropriate.
        logger.error("Error updating user account metadata:", error);
  
        // Return a dummy UserUpdateAccount object in case of an error
        return {
          userId: "",
          username: "",
          displayName: "",
          timezone: "",
          location: "",
          langTag: "",
          avatarUrl: "",
          metadata: {},
        };
      }
    };
  
    // Register the afterUpdateAccount hook
    initializer.registerAfterUpdateAccount(afterUpdateAccount);
  
    // Define the rpcCreateNewAccount function
    function rpcCreateNewAccount(
      ctx: nkruntime.Context,
      logger: nkruntime.Logger,
      nk: nkruntime.Nakama,
      payload: any
    ): any {
      // Your rpcCreateNewAccount function implementation here...
    }
  
    // Register the rpcCreateNewAccount function
    initializer.registerRpc("updateaccount", rpcCreateNewAccount);
  
    logger.info("JavaScript logic loaded.");
  }
typescript server-side rpc nakama
© www.soinside.com 2019 - 2024. All rights reserved.