loopback - 从另一个模型上传文件
loopback - Upload files from another model
我有两个模型:
容器:
处理文件上传并在环回资源管理器中正常工作。
爬虫:
有多个属性。属性之一是 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(...)
函数上冻结并且没有 return 任何东西.一段时间后,请求将因超时而中止。
我在 Whosebug 上找到了 ,但那里也发生了同样的问题。
upload
函数发生了什么变化?传递给 upload
的参数是否正确?有什么解决方法吗?除了 loopback-component-storage
?
之外,还有其他上传文件的解决方案吗?
(我的node版本是6.7.0,loopback 2)
首先,你应该像这样使用 body-parser 中间件:
"parse": {
"body-parser#json": {},
"body-parser#urlencoded": {"params": { "extended": true }}
}
BTW 文件以这样的数组形式发送:fileObject.files[null]
我有两个模型:
容器: 处理文件上传并在环回资源管理器中正常工作。
爬虫:
有多个属性。属性之一是 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(...)
函数上冻结并且没有 return 任何东西.一段时间后,请求将因超时而中止。
我在 Whosebug 上找到了
upload
函数发生了什么变化?传递给 upload
的参数是否正确?有什么解决方法吗?除了 loopback-component-storage
?
(我的node版本是6.7.0,loopback 2)
首先,你应该像这样使用 body-parser 中间件:
"parse": {
"body-parser#json": {},
"body-parser#urlencoded": {"params": { "extended": true }}
}
BTW 文件以这样的数组形式发送:fileObject.files[null]