【发布时间】: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