How to get current_user_id from wordpress in node js?

First, you should not be modifying any database values by allowing users to send an AJAX call to your server specifying the user ID to use, that would allow anybody to send an AJAX call with the nonce to modify any user’s data.

If you want to make an AJAX call to get the user’s ID it should be like this:

add_action( 'wp_ajax_cur_user_id', 'get_cur_user_id' );
function get_cur_user_id() {

    check_ajax_referer( 'wt_nonce', 'wt_nonce_code' );
    $user_id = get_current_user_id();

    if( empty( $user_id ) ){
        wp_send_json_error( array( 'not_logged_in' => 'User is not logged in' ) );
    } else {
        wp_send_json_success( $user_id );
    }

    die();
}

But you shouldn’t be using that to send to your server to modify data, you should send the modified data to WordPress via AJAX and then use get_current_user_id to get the user’s ID, not allowing it to be sent via AJAX

UPDATE:

Here’s an example of how I would do this, making the AJAX call from node, not allowing the user id to be passed, but checking it internally in WordPress.

This assumes that you would send a POST in AJAX with the amount to buy in buy or amount to sell in sell

This also assumes that balance is saved in the user’s meta

add_action( 'wp_ajax_update_cur_user_balance', 'update_cur_user_balance' );
function update_cur_user_balance() {
    global $wpdb;

    check_ajax_referer( 'wt_nonce', 'wt_nonce_code' );
    $user_id = get_current_user_id();

    if ( empty( $user_id ) ) {
        wp_send_json_error( array( 'not_logged_in' => 'User is not logged in' ) );
        return;
    }

    $buy  = array_key_exists( 'buy', $_POST ) ? floatval( $_POST['buy'] ) : false;
    $sell = array_key_exists( 'sell', $_POST ) ? floatval( $_POST['sell'] ) : false;

    $user_balance = get_user_meta( $user_id, 'user_balance', true );
    $user_balance_float = $user_balance ? floatval( $user_balance ) : 0;
    $transacted = false;

    if( $buy ){

        if( $buy > $user_balance_float ){
            wp_send_json_error( array( 'not_enough_funds' => 'Not enough funds to buy!' ) );
            return;
        }

        // Subtract buy amount from balance
        $user_balance_float = $user_balance_float - $buy;
        $transacted = true;
    } elseif( $sell ){

        // Add to user balance
        $user_balance_float = $user_balance_float + $sell;
        $transacted = true;
    }


    if( $transacted ){
        update_user_meta( $user_id, 'user_balance', $user_balance_float );
        wp_send_json_success( array( 'completed' => 'Transaction Completed', 'new_balance' => $user_balance_float ) );
        return;
    }

    wp_send_json_error( array( 'no_transact' => 'No transaction was completed!' ) );
    die();
}

You of course would need to come up with some way to make sure that user’s don’t just POST their own buy/sell using your nonce, but that would be outside the scope of this question.

Or if doing a SQL query, something like this:

 // For doing a custom SQL query in WordPress (just an example, needs to match table, field, value, etc)
$some_table="your_table";
$something = '123456789';
$query = $wpdb->prepare( "UPDATE `%s` SET something='%s' WHERE `userid` = %d", array( $some_table, $something, $user_id ) );
$wpdb->query( $query );