【问题标题】:Laravel 8 Query Works locally but not on serverLaravel 8 查询在本地工作,但不在服务器上
【发布时间】:2021-10-03 15:47:28
【问题描述】:

我正在调试 Laravel 8 应用程序上的 API 调用,该应用程序在标头中使用 _token 参数。以下查询在本地返回结果:

$user= User::where('_token',$token)->first();

但不是在生产中。本地环境和生产环境都连接到同一个数据库(AWS RDS Aurora MySQL)。

我想知道是什么导致了这个问题,或者如何调试它。谢谢

本地

生产

代码

function getJson($fileName,Request $request){
        $user=$this->verifyToken($request->header('_token'));
        if(!isset($user->id)){  
            return response()->json(['error_code'=>'201', "error_message" => 'please enter valid user token']); 
        }
        try {
            $data=[];
            $files = File::allFiles(public_path('json-data/'.$fileName)); 

            foreach($files as $path){
                          // print_r($path);
                $file = pathinfo($path);
                $dirname= $file['dirname'] ;
                $basename= $file['basename'] ;
                $extension= $file['extension'] ;
                $filename= $file['filename'] ;

                if($fileName=='general'){
                    $data=  json_decode(strtolower(file_get_contents(public_path('json-data/'.$fileName.'/'.$basename))));
                }else{
                    $d=(array)json_decode(strtolower(file_get_contents(public_path('json-data/'.$fileName.'/'.$basename))));
                    $d=array_unique($d);
                    $data=  (array_merge($data,$d)); 
                }

            }                
                       // $data=(array)$data;
            return response()->json(['error_code'=>200, "error_message" => '','data'=>  ($data)]);
        } catch (\Exception $e) {
            return response()->json(['error_code'=>'204', "error_message" => $e->getMessage()]);
        }
    }

      

public function verifyToken($token=''){
            try {
                if($token != ''){
                    $user= User::where('_token',$token)->first();
                    return $user;
                }
            } catch (\Exception $e) {
                return ['error_code'=>'204', "error_message" => $e->getMessage()];

            }
          }

路线

Route::post('get-json/{fileName}','apiController@getJson');

【问题讨论】:

  • 检查生产中的令牌值 - 我认为,问题不在查询中。
  • 您必须调试正在运行的 SQL。在查询之前通过DB::enableQueryLog() 打开 SQL 日志记录,然后在查询之后通过DB::getQueryLog() 来查看正在运行的内容
  • 请提供两个场景的详细信息,即:请求详细信息,包括标头
  • 你能从 prod 上的 aurora 获取其他数据吗?这是您唯一无法获得的数据吗?
  • 请您显示所有控制器代码(或至少显示负责路由的方法)。 201 HTTP 响应表示资源已创建。

标签: php laravel laravel-8


【解决方案1】:

解决方案

以防它帮助任何人。检查生产中的值有助于调试问题。标题名称有下划线。 _token 删除_ 或通过参数传递_token 可以解决问题。显然 AWS Beanstalk 不接受标题中的下划线。

相关问题 Elastic Beanstalk doesn't accept headers from Axios

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-26
    • 2021-12-28
    • 1970-01-01
    • 2019-03-16
    • 2017-07-04
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    相关资源
    最近更新 更多