title: How to save byte arrays with Mongoose and Nodjs?
body: <p>I would like to save a string as a buffer using Mongoose and NodeJS.</p>
  <p>For example I'm sending this data to the server:</p>
  <pre><code>{
      &quot;timestamp&quot;: &quot;Sun Aug 07 2022 18:54:07 GMT+0300 (IDT)&quot;,
      &quot;logFileData&quot;: &quot;Hello World!\nThis is a test :)&quot;
  }
  </code></pre>
  <p>I created a model for my object:</p>
  <pre><code>const LogFileSchema = new mongoose.Schema({
      date: { type: Date },
      data: { type: Buffer }
  });
  </code></pre>
  <p>Then, in my router file I try to save the data like this:</p>
  <pre><code>const { timestamp, logFileData} = request.body;
      const utf8Encode = new TextEncoder();
      const byteArr = utf8Encode.encode(logFileData);
  
      const logFile = new LogFile({
          date: timestamp,
          data: byteArr
      });
  
      logFile.save()
  .then((data:any) =&gt; {
      console.log('logFileRouter: logFile save success');
      response.json(&quot;success&quot;)
  })
  .catch((e:any) =&gt; {
      console.log('logFileRouter: logFile save error', e);
      response.json(&quot;error saving log file&quot;)
  });
  </code></pre>
  <p>I get an error stating that:</p>
  <pre><code>logFileRouter: logFile save error Error: LogFile validation failed: data: Cast to Buffer failed for value &quot;Uint8Array(30) [
     72, 101, 108, 108, 111,  32, 87,
    111, 114, 108, 100,  33,  10, 84,
    104, 105, 115,  32, 105, 115, 32,
     97,  32, 116, 101, 115, 116, 32,
     58,  41
  ]&quot; (type Uint8Array) at path &quot;data&quot;
      at ValidationError.inspect
  </code></pre>
  <p>What is wrong with my code? How can I save data as Buffer correctly?</p>