FormData为空Javascript(提取API)

时间:2018-07-20 06:16:01

标签: javascript firebase fetch google-cloud-functions form-data

我尝试通过引用其他可用的答案来添加processData: false, contentType: false,但仍然无法正常工作。

这是我在做什么,

var postData = new FormData();
postData.append('id', dt.id);
postData.append('title', dt.title);
postData.append('location', dt.location);
postData.append('rawLocationLat', dt.rawLocation.lat);
postData.append('rawLocationLng', dt.rawLocation.lng);
postData.append('file', dt.picture, dt.id + '.png');
fetch('FIREBASE FUNCTION URL', {
  method: 'POST',
  body: postData,
  processData: false, contentType: false
})
.then(function (res) {
  console.log('Sent data', res);
  if (res.ok) {
    res.json()
      .then(function (resData) {
        deleteItemFromData('sync-posts', resData.id);
      });
  }
})
.catch(function (err) {
  console.log('Error while sending data', err);
});

服务器代码:

exports.storePost = functions.https.onRequest(async function (request, response) {
  await cors(request, response, function () {
    console.log("1.Request Got");
    var uuid = UUID();
    var formData = new formidable.IncomingForm();
    formData.parse(request, function (err, fields, files) {
      fs.rename(files.file.path, '/tmp/' + files.file.name);
      var bucket = gcs.bucket("BUCKETNAME");
      bucket.upload('/tmp/' + files.file.name, {
        uploadType: 'media',
        metadata: {
          metadata: {
            contentType: files.file.type,
            firebaseStorageDownloadTokens: uuid
          }
        }
      }, function (err, file) {
        console.log("Got FIle",file);
        if (!err) {
          admin.database().ref('posts').push({
              id: fields.id,
              title: fields.title,
              location: fields.location,
              rawLocation: {
                lat: fields.rawLocationLat,
                lng: fields.rawLocationLng
              },
              image: 'https://firebasestorage.googleapis.com/v0/b/' + bucket.name + '/o/' + encodeURIComponent(file.name) + '?alt=media&token=' + uuid
            })
            .then(function (success) {
              response.status(200).json({
                message: success
              });            })
            .catch(function (err) {
              response.status(500).json({
                error: err
              });
            });
        } else {
          console.log(err);
        }
      });
    });
  });
});

我已经检查了所有内容,配置没有任何问题,我正在Firebase的控制台中登录

1.Request Got

TypeError: Cannot read property 'path' of undefined
    at /user_code/index.js:32:27
    at IncomingForm.<anonymous> (/user_code/node_modules/formidable/lib/incoming_form.js:104:9)
    at emitOne (events.js:96:13)
    at IncomingForm.emit (events.js:188:7)
    at IncomingForm._error (/user_code/node_modules/formidable/lib/incoming_form.js:298:8)
    at IncomingMessage.<anonymous> (/user_code/node_modules/formidable/lib/incoming_form.js:122:12)
    at emitNone (events.js:86:13)
    at IncomingMessage.emit (events.js:185:7)
    at abortIncoming (_http_server.js:281:11)
    at Socket.socketOnEnd (_http_server.js:434:7)

数据: enter image description here

0 个答案:

没有答案