Skip to content

[Bug] triggerServerCallback crashes when server returns null #76

@RahulOmegalul

Description

@RahulOmegalul

When using triggerServerCallback from the TypeScript/JavaScript wrapper, the callback handler crashes with TypeError: Cannot read properties of null (reading '0') when the server-side callback returns nil/null.

Affected File

client/resource/callback/index.js (line 34)

Current Code (Buggy)

pendingCallbacks[key] = (args) => {
if (args[0] === 'cb_invalid') // ❌ Crashes if args is null
reject(callback '${eventName} does not exist);
resolve(args);
};

Proposed Fix

pendingCallbacks[key] = (args) => {
if (args && args[0] === 'cb_invalid') // ✅ Null-safe check
reject(callback '${eventName} does not exist);
resolve(args);
};

Steps to Reproduce

  1. Create a server-side callback that returns nil:
    lib.callback.register('test:callback', function(source)
    return nil -- Returns nil when no data exists
    end)

  2. Call it from TypeScript/JavaScript client:
    import { triggerServerCallback } from '@communityox/ox_lib/client';

const result = await triggerServerCallback('test:callback', 5000);

  1. Error occurs: TypeError: Cannot read properties of null (reading '0')

Expected Behavior

The callback should gracefully handle null returns and resolve with null instead of crashing.

Environment

  • Package Version: 3.32.1
  • FiveM Server: Latest
  • Build Tool: esbuild

Impact

This is a critical bug that causes crashes in any resource using callbacks that may return null, which is common for database queries that find no results or optional data lookups.

Additional Notes

The Lua version of ox_lib handles nil returns correctly. This is only an issue in the JS/TS NPM package where the callback response handler doesn't check if args exists before accessing args[0].

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions