Insert data in db in POST method gives error code 0 Slim v4

Hello,I’m new to this framework and I´m following a some years old Angular course and can’t make this POST method work. While building an Slim 4 API REST I need to insert json data in my db from a form I simulate on Postman.

$app->post('/products', function (Request $request, Response $response,$db): Response {
    $data = $request->getParsedBody();
    
    $html = var_export($data, true);
    $response->getBody()->write($html);
    $data2 = implode($data);
    $decode = json_decode($data2, true);
    //display json arrays content
    var_dump($decode);

 $query = "INSERT INTO productos VALUES (NULL,".
            "'{$decode['nombre']}',".
            "'{$decode['description']}',".
            "'{$decode['precio']}',".
            "NULL".
            ");";
var_dump($query);
$insert = $db->query($query);

 if($insert) {
        $result = array(
            'status' => 'success',
            'code' => 200,
            'message' => 'Product created'
        );
    }
    $resultado = implode( $result);
    $decode2 = json_decode($resultado, true);
    var_dump($decode2);

    return $response;
});

The $db variable contains a db connection:

$db= new mysqli('localhost', 'root','','db_name');

When executed the POST method with I receive Call to a member function query() on array error code 0 in line 87." $insert = $db->query($query);"

The insert query looks like it’s correct but maybe this “query()” method is deprecated?
I have tried different methods from the official documentation but didn’t fixed it yet.

Thank you.

Have you tried to pass the $db variable like this?

$app->post('/products', function (Request $request, Response $response) use ($db): Response {
     // ...
}