上传两个图像时,console.log(parsedRes)仅返回一个对象,而不是(预期的)两个对象

时间:2018-08-23 06:21:31

标签: javascript react-native promise

因此,我试图从上传到云/服务器的两个图像中获取base64数据(URL),但是当我解析响应时,我只会得到1个对象而不是2个对象(即,第一个对象的URL)图片)。

我必须从根本上监督一些事情,但我看不到。欣赏建议。

能不能帮我解释一下下面的代码为什么不起作用?

[编辑:我已包含服务器代码。希望这可以使我所面临的问题更加清楚]

    .then(token => {
        authToken = token;
        return fetch("myappURL/storeImage", 
        {
            method: "POST",
            body: JSON.stringify({ 
                image: image.base64,
                coverImage: coverImage.base64
            }),
            headers: { Authorization: "Bearer " + authToken }
        });
    })
    .catch(err => {
        console.log(err);
        alert("Oops! Something went wrong, please try again1")
        dispatch(uiStopLoading());
    })
    .then(res => {
        if (res.ok) {
            return res.json();
        } else {
            throw(new Error());
         }
    })
    .then(parsedRes => {console.log(parsedRes)}); // I only get 1 result instead of 2 (why?!)

服务器端代码:

exports.storeImage = functions.https.onRequest((request, response) => {
return cors(request, response, () => {....
.then(decodedToken => {
      const body = JSON.parse(request.body);
      fs.writeFileSync("/tmp/uploaded-image.jpg", body.image, "base64", err => {
        console.log(err);
        return response.status(500).json({ error: err });
      });
      const bucket = gcs.bucket("myapp.appspot.com");
      const uuid = UUID();

      return bucket.upload(
        "/tmp/uploaded-image.jpg",
        {
          uploadType: "media",
          destination: "/places/" + uuid + ".jpg",
          metadata: {
              metadata: {
                  contentType: "image/jpeg",
                  firebaseStorageDownloadTokens: uuid
              }
          }
        },
        (err, file) => {
          if (!err) {
            return response.status(201).json({  // I get this returned
              imageUrl:
                "https://firebasestorage.googleapis.com/v0/b/" +
                bucket.name +
                "/o/" +
                encodeURIComponent(file.name) +
                "?alt=media&token=" +
                uuid,
              imagePath: "/places/" + uuid + ".jpg"
            });
          } else {
            console.log(err);
            return response.status(500).json({ error: err });
          }
        }
      );
  })
  .catch(error => {
    console.log("Token is invalid!");
    response.status(403).json({error: "Unauthorized"});
  })
.then(decodedToken => {
      const body = JSON.parse(request.body);
      fs.writeFileSync("/tmp/uploaded-coverImage.jpg", body.coverImage, "base64", err => {
        console.log(err);
        return response.status(500).json({ error: err });
      });
      const bucket = gcs.bucket("myapp.appspot.com");
      const uuid = UUID();

      return bucket.upload(
        "/tmp/uploaded-coverImage.jpg",
        {
          uploadType: "media",
          destination: "/coverPlaces/" + uuid + ".jpg",
          metadata: {
              metadata: {
                  contentType: "coverImage/jpeg",
                  firebaseStorageDownloadTokens: uuid
              }
          }
        },
        (err, coverFile) => {
          if (!err) {
            return response.status(201).json({  // I do NOT get this returned (why not?!)
              coverImageUrl:
                "https://firebasestorage.googleapis.com/v0/b/" +
                bucket.name +
                "/o/" +
                encodeURIComponent(coverFile.name) +
                "?alt=media&token=" +
                uuid,
              coverImagePath: "/coverPlaces/" + uuid + ".jpg"
            });
          } else {
            console.log(err);
            return response.status(500).json({ error: err });
          }
        }
      );
  })
  .catch(error => {
    console.log("Token is invalid!");
    response.status(403).json({error: "Unauthorized"});
  });
 });
});

0 个答案:

没有答案