在我的代码中,当函数抛出一个错误,例如,需要组织名称或者它已存在时,catch块正常工作。但是在成功的情况下,尽管插入了相应的组织,但我的状态为200,并显示错误消息。我的代码中有什么错误。
insertTenancy是Mycontroller中编写的函数。
public function insertTenancy($data,$user, $res)
{
$org_name = $data['org_name'];
$queryString = "SELECT * FROM organisation WHERE name = ? and user_id =?";
$query = $this->db->prepare($queryString);
$query->execute([$org_name,$user]);
if($query->fetch(\PDO::FETCH_ASSOC)){
//throw new Exception('This organisation is already in use');
return CCommon::prepareError($res, 'This organisation is already in use', 403);
}
$insertOrg = "INSERT INTO organisation (currency_id,name,user_id) VALUES (1,:name,:user_id)";
$insertOrgname = $this->db->prepare($insertOrg);
$insertOrgname->bindParam(':name', $org_name);
$insertOrgname->bindParam(':user_id', $user_id);
$insertOrgname->execute();
$lastorgId = $this->db->lastInsertId();
return $lastorgId;
}
跟随routerContainer编写的函数
try
{
$postData = $request->getParsedBody();
$currentUser = $this->user;
$data = array();
$data = $this->MyController->insertTenancy($postData,$currentUser,$response);
echo json_encode($data);
exit;
}
catch (Exception $e)
{
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
return CBCommon::prepareError($response, $data['message'], 403);
exit;
}
答案 0 :(得分:0)
You create a new array then you add false to the index 'success'.
So why should
$data['message']
be defined?
Maybe you want change your
$error
into
$data['message']