S
S
Seva2018-11-14 13:34:57
JavaScript
Seva, 2018-11-14 13:34:57

Create order in Loopback?

Hello,
there is a function that hangs on the create hook

async function addPhotos (models, product, id) {
    if (product.photos) {
      const photos = product.photos.filter(photo => !photo.id)
      console.log(photos) 
      /* 
      [ { src: 'b196125b-4d49-4aca-9805-fbe7139f7266.jpg' },
        { src: '04722c5c-b5c8-42a8-ac7a-bd3f89653acf.jpg' },
        { src: '786b0ef2-390e-4082-8796-582a6716752f.jpg' } ]
      */

      await models.Photo.create(photos.map(photo => {
        return {
          src: photo.src,
          productId: id
        }
      }), function(err, obj) {
        console.log(obj)
        /*
        [ { src: 'b196125b-4d49-4aca-9805-fbe7139f7266.jpg',
            productId: 21,
            id: 49 },
          { src: '04722c5c-b5c8-42a8-ac7a-bd3f89653acf.jpg',
            productId: 21,
            id: 48 },
          { src: '786b0ef2-390e-4082-8796-582a6716752f.jpg',
            productId: 21,
            id: 50 } ]
        */
        if (err) {
          throw new Error(err)
        } else {
          return true
        }
      })
    } else {
      return true
    }
  }

For some reason, records are inserted into the table in the wrong order! What is it, how to treat it? Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question