Slim - Trying to get Property 'password' of non-object in

Asked

Viewed 65 times

0

Good night.

I’m doing a job for college that involves logging in..

I’m using the post to pull the requisitions and is giving me the following error:

<br />
<b>Notice</b>: Trying to get property 'senha' of non-object in
<b>C:\wamp64\www\programacao-internet-2\biblioteca_\UsuarioController.php</b> on line <b>28</b><br />

Post type, the url: localhost:8080/api/login and in the body:

{
    "email":"[email protected]",
    "senha": 5667
}

My Usuariocontroller.php file looks like this:

public function autenticar($request, $response, $args){
            $body = $request->getParsedBody();

            $dao = new UsuarioDAO;

            $usuario = $dao->buscarPorEmail($body['email']);
            if($usuario->senha == $body['senha']){
                $token = array(
                    'user' => strval($usuario->id),
                    'nome' => $usuario->nome
                );

                $jwt = JWT::encode($token, $this->key);
                return $response->withJson(['token' =>$jwt, 201])
                                ->withHeader('Content-Type', 'application/json');
            }else{
                return $response->withStatus(401);
            }
        }

Can someone help me? Thanks in advance!

  • This error means that you are trying to access an attribute of a variable that is not an object. From the looks of it, the error is in the "searchPorEmail()" function, because that’s where you’re trying to extract attributes from an object.

  • Hello Thaty, your question lacks details, it is important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] problem and attempt to solution. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English. That way we can help you.

1 answer

1

I believe the problem lies in your DAO.

The error must be in $usuario->senha, probably your method buscarPorEmail() is not returning an object but an array of objects.

Check in your file User if the method buscarPorEmail() is using fetch(PDO::FETCH_OBJ) instead of fetchAll(PDO::FETCH_OBJ), if it is fetchAll, even if only one result, the return will be an array, then you would change if($usuario->senha == $body['senha']){ for if($usuario[0]->senha == $body['senha']){

  • My searchPorEmail function, ta so: public Function searchPorEmail($email){ $selectEmail = 'SELECT * FROM clients WHERE email=:email'; $Pdo = Connection:::getConnected(); $c = $Pdo->prepare($selectEmail); $c->bindParam ('email', $email); $c->execute(); $c->setFetchMode(PDO::FETCH_ASSOC|PDO::FETCH_PROPS_LATE); $obj = $c->fetch(); Return $obj; }

  • Swap lines: $c->setFetchMode(PDO::FETCH_ASSOC|PDO::FETCH_PROPS_LATE); e $obj = $c->fetch(); By: $c->fetch(PDO::FETCH_OBJ);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.