midway icon indicating copy to clipboard operation
midway copied to clipboard

NotFoundError: uploadController is not valid in current context

Open liuyanzhi1214 opened this issue 3 years ago • 11 comments

  • Node Version:
  • Egg Version:
  • Plugin Name:
  • Plugin Version:
  • Platform:
  • Mini Showcase Repository:

使用midway/faas复现egg的文件上传功能 const fs = require('mz/fs'); import { basename } from 'path'; import { Context } from 'egg'; import { Controller, Inject, Post } from '@midwayjs/decorator'; @Controller('/') export class uploadController {

@Inject() ctx: Context;

@Post('/upload') async uplaodFile() { const file = this.ctx.request.files[0]; const name = 'upload/' + basename(file.filename); let result; try { // 处理文件,比如上传到云端 result = await this.ctx.oss.put(name, file.filepath); } finally { // 需要删除临时文件 await fs.unlink(file.filepath); }

return {
  url: result.url,
  // 获取所有的字段值
  requestBody: this.ctx.request.body,
};

} }

出现报错: NotFoundError: uploadController is not valid in current context at MidwayContainer.getAsync (E:\Midway\faas_downupload\node_modules@midwayjs\core\dist\context\applicationContext.js:175:19) at MidwayContainer.getAsync (E:\Midway\faas_downupload\node_modules@midwayjs\core\dist\context\midwayContainer.js:475:33) at MidwayRequestContainer.getAsync (E:\Midway\faas_downupload\node_modules@midwayjs\core\dist\context\requestContainer.js:78:32) at MidwayFaaSFramework.invokeHandler (E:\Midway\faas_downupload\node_modules@midwayjs\faas\dist\framework.js:169:56) at E:\Midway\faas_downupload\node_modules@midwayjs\faas\dist\framework.js:117:51 at dispatch (E:\Midway\faas_downupload\node_modules\koa-compose\index.js:42:32) at E:\Midway\faas_downupload\node_modules\koa-static-cache\index.js:39:69 at dispatch (E:\Midway\faas_downupload\node_modules\koa-compose\index.js:42:32) at E:\Midway\faas_downupload\node_modules\koa-compose\index.js:34:12 at E:\Midway\faas_downupload\node_modules@midwayjs\faas\dist\framework.js:123:39 { [Symbol(#NotFoundError)]: Symbol(#NotFoundError) }

如何解决呢?

liuyanzhi1214 avatar Jul 14 '21 09:07 liuyanzhi1214

uploadController 缺少 @Provide() 装饰器。

czy88840616 avatar Jul 14 '21 15:07 czy88840616

@czy88840616 faas支持上传了?

ZQun avatar Jul 15 '21 10:07 ZQun

有限制,少量上传或者oss前端直传

czy88840616 avatar Jul 15 '21 11:07 czy88840616

uploadController 缺少 @provide() 装饰器。

按照您说的修改了,可是又有新的问题Orz TypeError: Cannot read property '0' of undefined at uploadController.uplaodFile (E:\Midway\Midway\faas\src\function\upload.ts:16:40) at MidwayFaaSFramework.invokeHandler (E:\Midway\Midway\faas\node_modules@midwayjs\faas\dist\framework.js:174:56) at processTicksAndRejections (internal/process/task_queues.js:95:5) at FCRuntime.invokeHandlerWrapper (E:\Midway\Midway\faas\node_modules@midwayjs\runtime-engine\dist\lightRuntime.js:19:28) at invoke (E:\Midway\Midway\faas\node_modules@midwayjs\serverless-app\dist\framework.js:240:36)

liuyanzhi1214 avatar Jul 17 '21 09:07 liuyanzhi1214

或者faas是否支持使用 formidable 来实现文件上传?

liuyanzhi1214 avatar Jul 19 '21 06:07 liuyanzhi1214

函数无法支持 formidable 上传,函数网关 + 弹性本身限制,无法越过。

czy88840616 avatar Jul 19 '21 06:07 czy88840616

采用multer中间件的方式呢?

liuyanzhi1214 avatar Jul 20 '21 02:07 liuyanzhi1214

这个跟中间件没关系吧,核心原因是网关会把2m以内上传的文件,做base64编码,没有流式机制。

czy88840616 avatar Jul 20 '21 02:07 czy88840616

koa-hooks-react是否也支持使用koa的接口来实现文件上传呢?

liuyanzhi1214 avatar Jul 23 '21 05:07 liuyanzhi1214

koa-hooks-react是否也支持使用koa的接口来实现文件上传呢?

理论上是可行的,还没有试过。后续我会将这些周边的需求和方案梳理一遍

Lxxyx avatar Jul 23 '21 07:07 Lxxyx

koa-hooks-react中为什么无法解析url中的参数 例如:http://127.0.0.1:3000/api/download?name=testFile.rar 似乎将download?name=testFile.rar当作路由来识别 export default async () => { const ctx = useContext<Context>(); let arg = url.parse(ctx.url).query; let params = querystring.parse(arg); let name = params.name; console.log(name) const path = download/${name}; //const path = download/testFile.rar; ctx.attachment(path); await send(ctx, path); };

liuyanzhi1214 avatar Jul 25 '21 13:07 liuyanzhi1214