0

I have this method using Loopback(Node.Js framework) to return a JSON composed of MP3 and videos.

This is the code I have so far

  let names = await app.models.cm_comediens.getValue();
    let sonMp3 = await app.models.cm_comediens.getMp3ById(id);

    const mp3 = sonMp3.map(id  =>  {
      let mappedObj = {}
      Object.keys(id).forEach( async (key)=> {
        let medias = await app.models.cm_comediens_medias.find({ where: {idMedia: id[key].idMedia}} )
        mappedObj[key] = {
          "Titre": medias.map(person =>  person.intitule ).toString(),
          "Filename": medias.map(person =>   person.filename ).toString(),
          "Langue": names.find(y => y.idValue === id[key].qfLangue).fr,
          "Type": names.find(y => y.idValue === id[key].qfType).fr,
          "Description": id[key].description,
          "Intention1": names.find(y => y.idValue === id[key].qfInterpretation1).fr,
          "Intention2": names.find(y => y.idValue === id[key].qfInterpretation2).fr,
          "Intention3": names.find(y => y.idValue === id[key].qfInterpretation3).fr,
        }
      });
      return mappedObj;
    });

    let video = await app.models.cm_comediens.getVideoById(id);

    const videoss = video.map(id => {
      let mappedObj = {}
      Object.keys(id).forEach( async (key)=> {
         let medias = await app.models.cm_comediens_medias.find({ where: {idMedia: id[key].idMedia}} )
        mappedObj[key] = {
          "Titre": medias.map(person =>  person.intitule ).toString(),
          "Filename": medias.map(person =>   person.filename ).toString(),
          "Langue": names.find(y => y.idValue === id[key].qfLangue).fr,
          "Type": names.find(y => y.idValue === id[key].qfType).fr,
          "Description": id[key].description,
          "Intention1": names.find(y => y.idValue === id[key].qfInterpretation1).fr,
          "Intention2": names.find(y => y.idValue === id[key].qfInterpretation2).fr,
          "Intention3": names.find(y => y.idValue === id[key].qfInterpretation3).fr,
        }
      });
      return mappedObj;
    });

    result.mp3=mp3;
    result.video=videoss;

   var resultfinal = [result]
   return resultfinal;

The problem is when I run it it returns this :

enter image description here enter image description here

when I remove let medias = await app.models.cm_comediens_medias.find({ where: {idMedia: id[key].idMedia}} ) and the "Titre" and "FileName" elements from video everything works fine and the results are shown correctly for videos also.Also, when I do a console.log(medias) the results are there like this :

[ { idMedia: 42085,
    idComedien: 1426,
    intitule: 'Tale of us Distante',
    typeMedia: 'video',
    filename: 'http://localhost:3000/Who Loves The Sun (Original Mix).mp4',
    originalFilename: 'Who Loves The Sun (Original Mix).mp4',
    triComedien: 0,
    timestampCreation: 2019-06-13T10:56:03.000Z,
    timestampModification: 2019-06-13T10:56:03.000Z } ]
(node:13280) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
[ { idMedia: 42087,
    idComedien: 1426,
    intitule: 'cxwccw',
    typeMedia: 'video',
    filename: 'http://localhost:3000/Who Loves The Sun (Original Mix).mp4',
    originalFilename: 'Who Loves The Sun (Original Mix).mp4',
    triComedien: 0,
    timestampCreation: 2019-06-13T11:15:11.000Z,
    timestampModification: 2019-06-13T11:15:11.000Z } ]

so I don't know exactly what's wrong. Is it because I am calling the same function twice ?? any help would be appreciated. Thank you

Ahmed
  • 107
  • 2
  • 3
  • 14
  • I strongly recommend not reusing variable names like `id`, the code listed is very confusing. I also suggest using better variable names like `originalId` or `mp3Id` – MattB Jun 18 '19 at 13:57
  • 1
    using `async`/`await` in `.forEach` function rarely makes sense – Jaromanda X Jun 18 '19 at 14:00
  • this: _everything works fine and the results are shown correctly for videos also_ is not possible if you remove the query filter. You may be getting every result returned, but given your use case of filtered results, the videos are NOT returned correctly. The filter is not finding any results with that parameter. You haven't stated what that parameter resolves to (`id[key].idMedia`) – Randy Casburn Jun 18 '19 at 14:04
  • Please take a look at this [jsFiddle](https://jsfiddle.net/ocyb70rn/). It doesn't answer your question, but shows a better approach to structuring your code. It also shows a more appropriate way to use async/await given your code. The mapper functionality likely won't work as I guessed at your data structure. I hope you find it instructive. – Randy Casburn Jun 18 '19 at 14:32

0 Answers0