简体   繁体   中英

Uncaught TypeError: b.toLowerCase is not a function

I am fetching some records :

$companies = \App\User::where('type', 'pet-salon')->orWhere('type', 'veterinarian')->get();
return response()->json($companies);

The data coming back is an array of objects:

[{
  id: 2, 
  type: "xxx", 
  deactivate: 0, 
  xxx: "Hello", 
  middle_name: "Mid",   
  lastname: "xxx",
  //...
}]

This is the jQuery typeahead code:

$('#getCompaniesForConnection').typeahead({
  source:  function (query, process) {
    return $.get('/all/companies', { query: query }, function (data) {
      return process(data);
    });
  }
});

The exception its giving me :

Uncaught TypeError: b.toLowerCase is not a function

And the results drop-down is not showing too, What am i missing here ?

Yes, you need to json_encode your companies.

$companies = \App\User::where('type', 'pet-salon')->orWhere('type', 
'veterinarian')->get();
$result = json_encode($companies ); // return this or echo

First, the PHP code looks like it's a laravel code, so you can just return the $companies variable like so:

$companies = \App\User::where('type', 'pet-salon')->orWhere('type', 'veterinarian')->get();
return $companies;

Since models and collections are converted to JSON when cast to a string, you can return Eloquent objects directly from your application's routes or controllers. And also, let's see the definition of the process function to be sure that's not where the error is coming from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM