【问题标题】:loopback - Upload files from another modelloopback - 从另一个模型上传文件
【发布时间】:2016-10-08 16:40:05
【问题描述】:

我有两个模型:

容器: 处理文件上传并在环回资源管理器中正常工作。

爬虫: 有多个属性。其中一个属性是script,用户可以将脚本文件上传到此模型。

crawler.json:

{
  "name": "Crawler",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "script": {
      "type": "string"
    },
    "startCount": {
      "type": "number",
      "default": 0
    },
    "created_at": {
      "type": "date"
    },
    "updated_at": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

crawler.js:

module.exports = function(Crawler) {
  Crawler.upload = function (id,req,res,cb) {
    var container = Crawler.app.models.container;
    container.getContainers(function(er,containers){
      console.log(containers);
      if (containers.some(function(e){return e.name == 'script';})) {
        container.upload(req,res,{container:"script"},cb);
      }else{
        container.createContainer({name: "script"}, function(er,c){
          container.upload(req,res,{container: "script"},cb);
        });
      }
    });
  Crawler.remoteMethod(
    'upload',
    {
      description: 'Uploads a script',
      accepts: [
        { arg: 'id', type: 'string', http: {source: 'path'}},
        { arg: 'req', type: 'object', http: { source:'req' } },
        { arg: 'res', type: 'object', http:{ source: 'res'} }
      ],
      returns: {
        arg: 'crawler', type: 'Crawler', root: true
      },
      http: {path: '/:id/script',verb: 'post'}
    }
  );
};

问题是当我运行POST /Crawlers/:id/script 时,程序冻结container.upload(...) 函数并且不返回任何内容。一段时间后,请求将因超时而中止。

我在 StackOverflow 上找到了this solution,但同样的问题也发生在那里。

upload 函数会发生什么?传递给upload 的参数是否正确?有什么解决方法吗?除了loopback-component-storage之外,还有其他上传文件的解决方案吗?

(我的节点版本是6.7.0和loopback 2)

【问题讨论】:

    标签: node.js api storage loopbackjs


    【解决方案1】:

    首先,你应该像这样使用 body-parser 中间件:

    "parse": {
        "body-parser#json": {},
        "body-parser#urlencoded": {"params": { "extended": true }}
    }
    

    BTW 文件以这样的数组形式发送:fileObject.files[null]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多