回送组件存储错误:请求中止

时间:2019-01-28 21:52:36

标签: node.js firebase loopback

我正在尝试配置将环回文件上传到Firebase存储。主要问题是我使用的是半年前可用的代码。

代码停止工作,出现以下错误:

error: Error: Request aborted
at IncomingMessage.<anonymous> (/dist/functions/node_modules/formidable/lib/incoming_form.js:122:19)
at IncomingMessage.emit (events.js:182:13)
at abortIncoming (_http_server.js:444:9)
at socketOnEnd (_http_server.js:460:5)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process.internalTickCallback (internal/process/next_tick.js:72:19)
info: POST /api/images/upload - - ms - -
error: Unhandled error for request POST /api/images/upload: Error: Request aborted
at IncomingMessage.<anonymous> (/dist/functions/node_modules/formidable/lib/incoming_form.js:122:19)
at IncomingMessage.emit (events.js:182:13)
at abortIncoming (_http_server.js:444:9)
at socketOnEnd (_http_server.js:460:5)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process.internalTickCallback (internal/process/next_tick.js:72:19)

我已经阅读了所有stackoverflow,但情况有所不同。我不使用multer和bodyParser。只是尝试以与以前相同的方式上载vie Ajax文件。

这是我的datasource.js配置:

"storage": {
  "name": "storage",
  "connector": "loopback-component-storage",
  "provider": "google",
  "keyFilename": "config/firebase-key.json",
  "projectId": "spoc",
  "nameConflict": "makeUnique",
}

model-config.json

 "container": {
    "dataSource": "storage",
    "public": true
},
"images": {
    "dataSource": "firebase",
    "public": true
  }

container.json

{
   "name": "container",
   "base": "Model",
   "idInjection": true,
   "options": {
     "validateUpsert": true
 },
   "properties": {},
   "validations": [],
   "relations": {},
   "acls": [],
   "methods": []
  }

image.json

{
  "name": "images",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "mixins": {
    "Owner": true
  },
  "properties": {
    "name": {
      "type": "string"
    },
    "type": {
      "type": "string"
    },
    "src": {
      "type": "string",
      "required": true
    },
    "storeId": {
      "type:": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

images.js

/**
 * Images endpoint logic.
 **/
import firebase from 'firebase-admin';

export default (Images) => {
    /**
     * Upload image handler.
     * @param {Object} err - image upload error object.
     * @param {Object} fileObj - file information object.
     **/
    const uploadHandler = (err, fileObj, cb, req) => {
        console.log(fileObj);
        if(err) cb(err);
        else {
            const files = fileObj.files['files'];
            const imageData = files.map(file => {
                return {
                    name: file.name,
                    type: file.type,
                    container: file.container,
                    storeId: req.accessToken.userId ? req.accessToken.userId : null,
                    src: `https://firebasestorage.googleapis.com/v0/b/${file.container}/o/${file.name}?alt=media`
                };
            });
            Images.create(imageData, (err, obj) => createImageHandler(err, obj, cb));
        }
    };

    /**
     * Create image metadata entry.
     * @param {Object} err - create image metadata error object.
     * @param {Object} obj - response context object.
     **/
    const createImageHandler = (err,obj, cb) => {
        if (err !== null) cb(err);
        else cb(null, obj);
    };

    /** Configuration object for upload method */
    const remoteMethodConfig = {
        description: 'Uploads a file',
        accepts: [
            {arg: 'req', type: 'object', 'http': {source: 'req'}},
            {arg: 'res', type: 'object', 'http': {source: 'res'}}, 
            { arg: 'options', type: 'object', http:{ source: 'query'} }
        ],
        returns: {
            arg: 'fileObject', type: 'object', root: true
        },
        http: {verb: 'post'}
    };

    /** Get current project id to define approrpiate firebase storage bucket.  */
    const projectId = firebase.instanceId().app.options.projectId;

    /** Upload image to Firebase storage bucket. */
    Images.upload = (req, res, options, cb) => {
        console.log(projectId);
        console.log(req.body);
        if(!options) options = {};
        req.params.container = `${projectId}`;
        // ctx.req.data = ctx.req.body;
        Images.app.models.container.upload(req, res, options, (err, fileObject) => {
            uploadHandler(err, fileObject, cb, req)
        });
    };

    Images.remoteMethod('upload', remoteMethodConfig);
};

因此,请求挂起,然后中止。核心问题是它在六月某个地方工作:(

0 个答案:

没有答案