title: JavaScript class with mongoose db
body: <p>I want to use a class (with name <code>Paste</code>) with the following methods:
  <code>Paste.create()</code> - Creates a new record in MongoDB using mongoose.
  <code>Paste.find()</code> - Finds a record in MongoDB using mongoose.</p>
  <p>This is my Paste.js:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const db = require("../models/schema");
  
  class Paste {
      constructor( id, content, title, description, language ) {
          this.id = id;
          this.content = content;
          this.title = title;
          this.description = description;
          this.language = language;
      }
  
  
      create() {
          db.create({
              id: this.id,
              content: this.content,
              title: this.title,
              description: this.description,
              language: this.language
           })
      }
  
  
      find(id) {
          const r = db.findOne({ id: id });
          if(r==undefined) return false;
          return {
              id: r.id,
              content: r.content,
              title: r.title,
              description: r.description,
              language: r.language
          }
      }
  }
  
  module.exports = Paste;</code></pre>
  </div>
  </div>
  </p>
  <p>And this is the code I'm using to use those methods: (it executes when a HTML button is clicked)</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const { MONGO_URI } = require("../config.json");
  const { generateKey } = await import('crypto');
  const mongoose = require("mongoose");
  const Paste = require("./classes/Paste");
  
  mongoose.connect(MONGO_URI, {
      useNewUrlParser: true, 
      useUnifiedTopology: true
  })
  
  registerPaste = () =&gt; {
      try {
  
          let title = null;
          if(document.getElementById("pasteTitle").value &gt; 20) {
          title = document.getElementById("pasteTitle").value.slice(0, 40) + `...`
          }
  
          let id = null;
          generateKey("aes", { length: 10 }, (err, key) =&gt; {
              if (err) throw err;
              id = key.export().toString("hex");
            })
          
          const p = new Paste(id, document.getElementById("pasteContent").value, document.getElementById("pasteTitle").value, document.getElementById("pasteDescrp").value, document.getElementById("pasteLang").value);
          p.create();
          window.location.replace(`${window.location.href}/${id}`);
      } 
      catch {
          console.error;
      }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>The html code for calling the <code>registerPaste()</code> function:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  &lt;button class="btn no-animation btn-success font-p create-btn" onclick="registerPaste()"&gt;Create paste&lt;/button&gt;</code></pre>
  </div>
  </div>
  </p>
  <p><strong>When the button is clicked, it does not redirect to the URL of the newly created paste...Hope someone helps!</strong></p>
  <h3>More details:</h3>
  <ul>
  <li>Node.JS version: 16.8.0</li>
  <li>I'm using expressjs to render the static HTML, CSS files.</li>
  </ul>