Back

REST queries

  • 0
  • Self Hosted
  • REST API
AwesomeTaco
16 May, 2024, 20:47

Appwrite 1.5.5

Goal of function: See if computer_id and action_id exist in completed collection. Issue: If computer_id or action_id is anywhere in any of the docs, it will return all docs. I was using relationships before for the action and device but had the same issue. Changed to string to see if issue continued and it did.

Computer1: aaa Computer2: bbb

Action1: 111 Action2: 222

Completed Docs: Doc1: deviceAction: aaa_111 Doc2: deviceAction: bbb_222

Function runs with aaa_111, returns Doc1 Function runs with aaa_222, returns Doc1 and Doc2

TypeScript
let device_action_value = format!("{}_{}", computer_id, action_id);
let query = json!({
    "values": [
        {
            "method": "equal",
            "attribute": "deviceAction",
            "values": device_action_value
        }
    ]
});
TL;DR
Developers are creating a function to check if computer_id and action_id exist in a completed collection using REST queries. The issue is that if either ID exists in any document, all documents are returned. They tried using relationships and switching to strings but encountered the same problem. The solution is to change the query method from "contains" to "equal" in order to accurately check for the specified values.
AwesomeTaco
16 May, 2024, 20:48

Function: pub async fn check_completed( computer_id: &str, action_id: &str, ) -> Result<bool, Box<dyn std::error::Error>> { let client = Client::new();

TypeScript
let device_action_value = format!("{}_{}", computer_id, action_id);
let query = json!({
    "values": [
        {
            "method": "contains",
            "attribute": "deviceAction",
            "values": device_action_value
        }
    ]
});

println!("Constructed Query: {}", query);

let request_url = format!(
    "{}/databases/{}/collections/{}/documents",
    URL, DATABASE_ID_ACTIONS, COLLECTION_ID_ACTIONS
);

let response = client
    .get(&request_url)
    .header("X-Appwrite-Project", PROJECT_ID)
    .header("X-Appwrite-Key", API_KEY)
    .header("Content-Type", "application/json")
    .json(&query)
    .send()
    .await?;

let status = response.status();
let response_text = response.text().await?;

println!("Response Status: {}", status);
println!("Response Text: {}", response_text);

if status.is_success() {
    let completed_data: serde_json::Value = serde_json::from_str(&response_text)?;
    if let Some(documents) = completed_data["documents"].as_array() {
        println!("Number of Documents Found: {:?}", documents.len());
        return Ok(!documents.is_empty());
    }
    Ok(false)
} else {
    Err(format!("Failed to fetch documents: {} - {}", status, response_text).into())
}

}

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more