I am trying to get User info from MongoDB like this:
pub async fn find_user_by_name(&self, data: String) -> Vec<Uye> {
let filter = doc! {"isim": data};
let options = FindOptions::builder()
.projection(doc! {"sifre": 0, "mail": 0, "tokenInfo": 0 })
.build();
let cursor = match self.user_col.find(None, options).await {
std::result::Result::Ok(cursor) => cursor,
std::result::Result::Err(_) => return vec![],
};
cursor.try_collect().await.unwrap()
}
My Uye struct is:
pub struct Uye {
pub id: Option<i32>,
pub isim: Option<String>,
pub sifre: Option<String>,
pub mail: Option<String>,
pub dogumtarihi: Option<String>,
pub avatar_url: Option<String>,
pub kaydedilen: Option<Vec<i32>>,
pub perm: Option<String>,
pub permLevel: Option<i32>,
pub onaylandi: Option<bool>,
pub isDummy: Option<bool>,
pub hesap_onay_kodu: Option<i32>,
pub avatar_path: Option<String>,
pub tokenInfo: Option<TokenInfo>,
}
When I get the data from the API with http request, the result is like this:
{
"status": "basarili",
"user": [
{
"id": 60,
"isim": "test User",
"sifre": null, // <-----
"mail": null, // <-----
"dogumtarihi": "",
"avatar_url": "/static/uploads/images/avatar",
"kaydedilen": [],
"perm": "Üye",
"permLevel": 0,
"onaylandi": false,
"isDummy": false,
"hesap_onay_kodu": 123123,
"avatar_path": "/static/uploads/images/avatar",
"tokenInfo": null // <-----
}
]
}
I want to delete null fields from object to make it look more better. Is there any way to it ?
You have quite a lot of fields in your struct. Might be worthwhile to use the serde_with
crate:
#[serde_with::skip_serializing_none]
#[derive(Serialize)]
pub struct Uye {
pub id: Option<i32>,
pub isim: Option<String>,
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With