1

I'm working on Scrumpoker with Vue js.

I have a model for users,sessions and these stored in my database with properties like sessionID, nameID, userID, etc.

I'm trying to let an user join in a room which is not created by him.

If I try to, I get an error message looking at the web dev panel saying:

POST http://127.0.0.1:8090/xdd/src/api/user/create.php 400 (Bad Request)
vue-resource.esm.js?f3ef:1082

When I load my local path url including the create.php it says:

Notice: Trying to get property 'userName' of non-object in C:\laragon\www\xdd\src\api\user\create.php on line 19

Notice: Trying to get property 'sessionId' of non-object in C:\laragon\www\xdd\src\api\user\create.php on line 20

Anyone could help me ? It's the last thing to fix (with playing cards) the biggest issues speaking about api's.

Here is my code of the user/create.php:

<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/db.php';
include_once '../models/user.php';

$database = new Database();
$db = $database->getConnection();

$user = new User($db);

$data = json_decode(file_get_contents("php://input"));

$user->userName = $data->userName;
$user->userSession = $data->sessionId;

if($_SERVER['REQUEST_METHOD'] == "OPTIONS") {
  echo "OPTIONS";
} else {
  switch($user->create()){
    case "NEW USER":
      $insertId = $db->lastInsertId();
      echo "$insertId";
      break;
    case "EXISTING USER":
      echo "$user->userId";
      break;
    case "SESSION NOT FOUND":
      http_response_code(404);
      break;
    default:
      http_response_code(400);
  }
}


?>
Request URL: http://127.0.0.1:8090/xdd/src/api/user/create.php
Request Method: POST
Status Code: 400 Bad Request
Remote Address: 127.0.0.1:8090
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Access-Control-Allow-Methods: POST
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Connection: close
Content-Length: 95
Content-Type: application/json; charset=UTF-8
Date: Wed, 11 Sep 2019 13:53:47 GMT
Server: Apache/2.4.35 (Win32) OpenSSL/1.1.0i PHP/7.2.11
X-Powered-By: PHP/7.2.11
Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: application/json;charset=UTF-8
Origin: http://localhost:8080
Referer: http://localhost:8080/join/32
Sec-Fetch-Mode: cors
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
{userName: "a", sessionId: "32"}
sessionId: "32"
userName: "a"
aynber
  • 22,380
  • 8
  • 50
  • 63
lafllamme
  • 71
  • 1
  • 2
  • 8
  • Well, obviously `$data` is not an object. `var_dump($data);` to find out what it contains. If it's false, `var_dump(file_get_contents("php://input"));` – aynber Sep 11 '19 at 13:42
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – aynber Sep 11 '19 at 13:42
  • @aynber (i)prints NULL (ii)prints string(0) "" – lafllamme Sep 11 '19 at 13:45
  • The `php://input`-stream gives you the body of the request. If you access the page directly through the browser, it does it with a GET method, which doesn't contain any body, which means that you're trying to `json_decode()` an empty value (which won't return an object). – M. Eriksson Sep 11 '19 at 13:45
  • @aynber I'm confused how am I able to fix this in manner of retrieving my neccessary data – lafllamme Sep 11 '19 at 13:45
  • How are you posting the data? – M. Eriksson Sep 11 '19 at 13:49
  • @MagnusEriksson What do I have to write then inside the brackets? I just started with PHP a while ago so am I not sure how to handle this – lafllamme Sep 11 '19 at 13:49
  • @MagnusEriksson through Vue with Vueressource – lafllamme Sep 11 '19 at 13:50
  • Apparently you're not getting any data from the request. So you need to make sure it's being sent correctly. Check the Network tab and see what is being sent. – aynber Sep 11 '19 at 13:51
  • @aynber ```object(stdClass)#4 (2) { ["userName"]=> string(6) "rfgdfs" ["sessionId"]=> string(2) "36" } ```` – lafllamme Sep 11 '19 at 13:55
  • @aynber I edited my post with the data that is submitted – lafllamme Sep 11 '19 at 13:56
  • `{userName: "a", sessionId: "32"}` is not valid JSON. – gre_gor Sep 11 '19 at 19:03

0 Answers0