1

I'm trying to make a post request that'll send file name, id and email to the database. This is working fine Postman but when I try on the browser. It's giving me ErrorException: Trying to get property 'id' of non-object in file error.

My steps on the browser:

  1. Login with user credentials. (successful)
  2. Trying to upload a file - I get the aforementioned error above. (not successful)

I'm just confused as to why it works in Postman but not on the browser. What am I doing wrong?

Note: The dd($id, $name); returns the correct values.

front end react code:

fileUpload(file) {
    const formData = new FormData()

    formData.append('file', file);
    formData.append('id', this.state.id);
    formData.append('email', this.state.email);

    fetch('http://myendpoint/api/auth/wall-of-fame', {
        method: 'POST',
        body: formData
    })
        .then(response => console.log(response))
        .catch(error => { console.error(error) });
}

controller code:

public function store(Request $request){
    $filePath = $request->file('file')->getClientOriginalName();
    $id = $request->user()->id;
    $email = $request->user()->email;

//  dd($id, $email);

    $data= [
        'file_path' => $filePath,
        'user_id' => $id,
        'email' => $email
    ];

    DB::table('my.db')->insert($data);
    echo "Record inserted successfully.<br/>";
}
cloudyday
  • 183
  • 6
  • 1
    `fetch` doesn't send along cookies by default, you need to specifically tell it to: https://stackoverflow.com/questions/34558264/fetch-api-with-cookie –  Jul 14 '20 at 18:17
  • First, don't use `echo` in your Controller code... You should return a proper HTTP response: `return response()->json(['message' => 'Record Inserted Successfully"]);` for example. – Tim Lewis Jul 14 '20 at 19:00
  • @TimLewis yeah will do, you're right. I'm still kinda stumped on the cookie stuff above though :\ – cloudyday Jul 14 '20 at 19:05
  • Ah, so is the line that's failing `$request->user()->id;`? Could you replace that with `auth()->user()->id`? If this is an AJAX request within an authenticated session, that _should_ work. However, if this is a stateless api call, then you'll have to send the user id (and email) in a different fashion. Edit: You're actually sending them already; why not `$request->input('id')` and `$request->input('email')`? – Tim Lewis Jul 14 '20 at 19:06
  • @TimLewis I've tried `$request->input('id')` and `$request->input('email')` but it returns `null` in Postman. – cloudyday Jul 14 '20 at 19:11
  • Well yeah, I would kind of expect that in Postman. `this.state.id` and `this.state.email` don't mean anything in Postman. In browser, those are JS variables. You'll probably have to show your Postman request, I suspect you're not actually sending anything for those input fields. – Tim Lewis Jul 14 '20 at 19:12
  • You should also be rockin a `tail -f storage/logs/laravel.log` when makin this request to see whats going on. – Michael Mano Jul 15 '20 at 05:51

0 Answers0