Skip to content

Cant insert poem-openapi Objects into Mongodb #609

Answered by sunli829
NexRX asked this question in Q&A
Discussion options

You must be logged in to vote
  1. convert the OpenAPI object to JSON
use poem_openapi::types::ToJSON;

pub fn create(&self, new_user: &User) -> Result<InsertOneResult, ErrorMessage> {
        self.col
            .insert_one(new_user.to_json(), None) // <<<<<<<<< convert new_user to JSON
            .map_err(|err| default_handling_mongo(*err.kind))
    }
  1. derive User by serde::Serialize
#[derive(Debug, Object, Clone, Eq, PartialEq, Serialize)] // <<<<<<<<<<<<< derive serde::Serialize
#[oai(skip_serializing_if_is_none, rename_all = "camelCase")]
pub struct User {
    #[oai(rename = "_id", read_only)]
    pub id: Uuid,

    #[oai(validator(min_length = 3, max_length = 16, pattern = r"^[a-zA-Z0-9_]*$"))]
    pub username: S…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@sunli829
Comment options

Answer selected by NexRX
@NexRX
Comment options

Comment options

You must be logged in to vote
1 reply
@NexRX
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants