lametro / programacion-4-2015

Programación 4
MIT License
1 stars 4 forks source link

Error para crear un usuario (WARNING: Illegal string offset 'name') #45

Closed fedemiguez closed 9 years ago

fedemiguez commented 9 years ago

Hola profe, estamos teniendo un error a la hora de crear un usuario es solo con esa consulta, el problema existe desde la app y desde el postman, antes funcionaba y despues de modificar la api empezo a tirar el problema

image

aca el error en postman

image

aca dejo el codigo de la api

<?php

ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1);

require 'vendor/autoload.php'; require 'Models/User.php';

$app = new \Slim\Slim();

$app->config('databases', [ 'default' => [ 'driver' => 'mysql', 'host' => 'us-cdbr-iron-east-03.cleardb.net', 'database' => 'heroku_b2562bab37645c3', 'username' => 'bd90c798d775ac', 'password' => '9221123b', 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '' ] ]);

$app->add(new Zeuxisoo\Laravel\Database\Eloquent\ModelMiddleware); $app->view(new \JsonApiView()); $app->add(new \JsonApiMiddleware()); $app->add(new \Slim\Middleware\ContentTypes());

$app->options('/(:name+)', function() use ($app) { $app->render(200,array('msg' => 'API SeJuega')); });

$app->get('/', function () use ($app) { $app->render(200,array('msg' => 'API SeJuega')); }); $app->get('/usuario', function () use ($app) { $db = $app->db->getConnection(); $users = $db->table('users')->select('id', 'name')->get(); $app->render(200,array('data' => $users)); }); $app->post('/usuario', function () use ($app) { $input = $app->request->getBody(); $name = $input['name']; if(empty($name)){ $app->render(500,array( 'error' => TRUE, 'msg' => 'name is required', )); } $password = $input['password']; if(empty($password)){ $app->render(500,array( 'error' => TRUE, 'msg' => 'password is required', )); } $email = $input['email']; if(empty($email)){ $app->render(500,array( 'error' => TRUE, 'msg' => 'email is required', )); } $user = new User(); $user->name = $name; $user->password = $password; $user->email = $email; $user->save(); $app->render(200,array('data' => $user->toArray())); }); $app->put('/usuario/:id', function ($id) use ($app) { $input = $app->request->getBody();

$name = $input['name'];
if(empty($name)){
    $app->render(500,array(
        'error' => TRUE,
        'msg'   => 'name is required',
    ));
}
$password = $input['password'];
if(empty($password)){
    $app->render(500,array(
        'error' => TRUE,
        'msg'   => 'password is required',
    ));
}
$email = $input['email'];
if(empty($email)){
    $app->render(500,array(
        'error' => TRUE,
        'msg'   => 'email is required',
    ));
}
$user = User::find($id);
if(empty($user)){
    $app->render(404,array(
        'error' => TRUE,
        'msg'   => 'user not found',
    ));
}
$user->name = $name;
$user->password = $password;
$user->email = $email;
$user->save();
$app->render(200,array('data' => $user->toArray()));

}); $app->get('/usuario/:id', function ($id) use ($app) { $user = User::find($id); if(empty($user)){ $app->render(404,array( 'error' => TRUE, 'msg' => 'user not found', )); } $app->render(200,array('data' => $user->toArray())); }); $app->delete('/usuario/:id', function ($id) use ($app) { $user = User::find($id); if(empty($user)){ $app->render(404,array( 'error' => TRUE, 'msg' => 'user not found', )); } $user->delete(); $app->render(200); }); ?>

catrielmuller commented 9 years ago

Para usar postman ahora que la API procesa JSON para ser compatible con Ionic, tienes que configuarlo de la siguiente manera. postman

Tambien te dejo un Screen del Chrome para que veas como ver el Payload que le envia al server.

fedemiguez commented 9 years ago

si con postman funciona bien configurando asi, pero no me sale eso del payload

image

que tendria que hacer para solucionarlo?

catrielmuller commented 9 years ago

Podrias indicarme el respositorio de tu app de ionic para ver el codigo del post.

fedemiguez commented 9 years ago

la app de ionic la tenemos en bitbucket https://bitbucket.org/sejuega/se-juega

catrielmuller commented 9 years ago

https://bitbucket.org/sejuega/se-juega/commits/1b2121b70c8220d3fd6a43851e9e2b967cdeac9e?at=master