2017-08-28 3 views
1

マイマングース機能が約束クエリを無視して、即座に私は思います別のステップマングースの約束作業

exports.editProduct = (id, data) => { 
    Product.findById(id) 
    .then((data) => { 
     var imgS3arr = data.img; 
     var tempArr = []; 
     var i = imgS3arr.length 
     while (i--) { 
      if (!imgS3arr[i].match(/https:\/\/s3.eu-central-1.amazonaws.com\/es-shop\//g)) { 
       tempArr.push(imgS3arr[i]) 
       imgS3arr.splice(i, 1) 
      } 
     } 
     return tempArr 
    }) 
    .then((tempArr) => { 
     var tempArrS3 = [] 
     return Promise.all(tempArr.map((img, i) => { 
      return fetch.remote(img).then((base) => { 
       var buf = Buffer.from(base[0], 'base64'); 
       var imgS3 = { 
        Key: data.title.replace(/()|(")/g, "_") + "_" + Math.random().toString(36).substring(2), 
        Body: buf, 
        ContentEncoding: 'base64', 
        ContentType: 'image/jpeg' 
       }; 
       return s3Bucket.putObject(imgS3).promise().then((data) => { 
        tempArrS3.push('https://s3.eu-central-1.amazonaws.com/es-shop/' + imgS3.Key) 
        console.log(tempArrS3) 
       }).catch((err) => { 
        throw err; 
       }); 
      }); 
     })) 
     .then((tempArrS3) => { 
      edited.title = data.title; 
      edited.img = imgS3arr.concat(tempArrS3); 
      return edited 
     }); 
    }) 
    .then((edited) => { 
     console.log(edited) 
     return edited.save(); 
    }); 
    } 

There is a point where I call this function

を待たずに、最後まで落下、約束を使用して、私はいくつかの缶右

いけないいけません1つはこのトラブルで私を助ける?

+0

あなたはどちらか持っている '' Product.findById(ID) 'や'(ID、データ)=> Product.findById(ID)を返す必要があり、あなたの外部関数定義がPromiseを返さないからです。最初の呼び出しが 'return'でなければ、チェーンの最後の項目からPromiseを返すことはできません。 –

+0

コードを書くときに、特にjavascriptを使ってください。コードを論理関数に分割します。コールバック/約束を使用すると、本当に速く読むことができなくなります。あなたはいくつかの '.catch' thoを見逃しました。これは問題になります。 –

答えて

2

あなたはeditProduct機能でreturnステートメントを呼び出すことを忘れ:

exports.editProduct = (id, data) => { 
    return Product.findById(id); 
    ... 
+0

ありがとう!最後に動作する –