试图获得非对象的财产 - laravel mongodb

时间:2017-03-20 05:55:42

标签: ajax mongodb laravel

我正在使用带有mongodb的laravel,当我得到结果时,它显示“试图获取非对象的属性”,我的代码出了什么问题?

Controller query:
$fetch_qry = Auditproject::where('projectid','=',$request->proj_id)->where('siteengineerid','=',$request->cont_id)->where('sub_id','=',$loggedin)->first();
$audit_pid=$fetch_qry->_id;

My ajax:
 datastring="mytext="+displayed_tree1+"&mytext1="+selected_node1+"&proj_id="+pj_id+"&cont_id="+site_engineer_id+"&wbs_display="+res;

              $.ajax
              ({
                  type: "POST",
                  dataType : 'json',
                  async:true,
                  beforeSend: function (xhr) {
                      var token = $('meta[name="csrf_token"]').attr('content');
                      if (token) {
                          return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                      }
                  },
                  url: "{{ URL::to('store_wbs_siteeng') }}",
                  data: datastring,
                  success:function(data){

                      return "Assigned Successfully";
                  },
                  failure: function() {alert("Error!");}
              }); 

1 个答案:

答案 0 :(得分:0)

如果您的查询返回对象或null

,您应该始终进行检查
$audit_pid = is_null($fetch_qry) ? $defaultId : $fetch_qry->_id;

或者:

if (is_null($fetch_qry)) {
    return response()->json(['Result is empty']);
}

如果结果为null,则只表示数据库中现在有projectid = $request->proj_idsiteengineerid = $request->cont_idsub_id = $loggedin

的行