0

I try currently to recording data in my database. I use base64 npm module to translate my blob in base64 standards then store it into the database.

But when I sent my records to my database, the Get/* methods returns me exactly the same raw text for each records. Note that I have made the records on differents session, not in the same sent-stream :

Here the return of my database :

[{"_id":"5b09c5a9db6839382475442b","sound":{"type":"Buffer","data":[87,50,57,105,97,109,86,106,100,67,66,67,98,71,57,105,88,81,61,61]},"__v":0},{"_id":"5b09c5b7db6839382475442c","sound":{"type":"Buffer","data":[87,50,57,105,97,109,86,106,100,67,66,67,98,71,57,105,88,81,61,61]},"__v":0},{"_id":"5b09c5c4db6839382475442d","sound":{"type":"Buffer","data":[87,50,57,105,97,109,86,106,100,67,66,67,98,71,57,105,88,81,61,61]},"__v":0},{"_id":"5b09c69bdb6839382475442e","sound":{"type":"Buffer","data":[87,50,57,105,97,109,86,106,100,67,66,67,98,71,57,105,88,81,61,61]},"__v":0}, (... and so on)

I wonder what can produce this behavior.

I try currently to directly retrieve the data URL and retrieve the binary text directly from it as - pseudo code :

 blob:http://localhost:[path]
-> then retrieve data in this path 
-> then store in database data retrieved

Meanwhile, here my App.js :

class RecordingAPI extends React.Component {
  constructor(props) {
    super(props);
    this.deleteAudio = this.deleteAudio.bind(this);
    this.handleSubmit=this.handleSubmit.bind(this);
    this.state = {
      recording: false,
      audios: [],
      blob : {}
    };
  }
  handleSubmit(e){
   e.preventDefault();


   Axios.post("/api/words",{
                           "sound":this.state.blob
                         })
   //.then((res) => res.json())
   .then((data) => console.log(data))
  //pass submitted value to true in order to declench allDelete function

    alert("Message sent, congratulation =)")
    //this.state.deleteAll();

 this.deleteAll();
 }

  async componentDidMount() {
    const stream = await navigator.mediaDevices.getUserMedia({audio: true});
    // show it to user
    this.audio.src = window.URL.createObjectURL(stream);
    this.audio.play();
    // init recording
    this.mediaRecorder = new MediaRecorder(stream);
    // init data storage for video chunks
    this.chunks = [];
    // listen for data from media recorder
    this.deleteAll=this.deleteAll.bind(this) ;
    this.mediaRecorder.ondataavailable = e => {
      if (e.data && e.data.size > 0) {
        this.chunks.push(e.data);
      }
    };
  }

  startRecording(e) {
    e.preventDefault();
    // wipe old data chunks
    this.chunks = [];
    // start recorder with 10ms buffer
    this.mediaRecorder.start(10);
    // say that we're recording
    this.setState({recording: true});
  }

  stopRecording(e) {
    e.preventDefault();
    // stop the recorder
    this.mediaRecorder.stop();
    // say that we're not recording
    this.setState({recording: false});
    // save the video to memory
    this.saveAudio();

  }

  saveAudio() {
    // convert saved chunks to blob
    const blob = new Blob(this.chunks, {type: audioType});
    // generate video url from blob
    const audioURL = window.URL.createObjectURL(blob);
    console.log(audioURL); 
    // append videoURL to list of saved videos for rendering
    const audios = this.state.audios.concat([audioURL]);
    this.setState({audios});
    var blob64 = Base64.encode(blob);
    this.setState({blob : blob64})

  }

Thanks.

Webwoman
  • 10,196
  • 12
  • 43
  • 87
  • You don't need to convert to Base64 as MongoDB will store the Binary data without problem. if you just did that then you would get Binary back straight away. What do you think you want it in Base64 for anyway? The best way to put an image in your page is and always has been with a URL refrerence to what it "believes" to be a file. You should only "inline" such data for fairly static assets, such as "logos" and standard decoration. Any images you need to store in a database should be retrieved as though they were a "file". – Neil Lunn May 26 '18 at 22:17
  • Thank Neil for your answer, so you advice me to go on the URL way means FS and all if I understand you – Webwoman May 26 '18 at 22:38
  • The common wisdom is for something that may regularly change there is no point sending base64 data or even embedding it within pre-rendered output. If a browser requests `/images/something.png` it will load the content ( and happily to a `src` tag ) and most importantly "cache" the content in case the same resource is requested again, in which case the request is not made again. Sending Base64 embedded in a JSON response or pre-rendered content removes that optimization. It's very simple to return the binary data from a database and ouput as a stream of data. Just like a "file" does. – Neil Lunn May 26 '18 at 22:53
  • thanks for theses precisions (y) Neil – Webwoman May 26 '18 at 23:52

0 Answers0