[英]Cannot define nor retrieve metadata on method
I'm trying to attach a metadata key-value pair to an object key (method in a TS class), but no action is actually being performed on the element.我正在尝试将元数据键值对附加到对象键(TS 类中的方法),但实际上没有对元素执行任何操作。
import 'reflect-metadata';
export const get = (path: string) => (target: any, key: string, desc: PropertyDescriptor) => {
Reflect.defineMetadata('path', path, target, key);
console.log(`path, target, key -> ${ path }, ${ target }, ${ key }`);
};
Snippet of the call section in the code代码中调用部分的片段
@controller('/auth')
export class LoginController {
@get('/login')
getLogin(req: Request, res: Response): void {/* method implementation */};
}
Controller iterating through methods metadata控制器迭代方法元数据
export const controller = (routePrefix: string) => {
return function (target: Function) {
for (let key in target.prototype) {
const routeHandler = target.prototype[key];
const path = Reflect.getMetadata('path', target.prototype[key]);
console.log('here is the path! -> ' + path);
if (path)
router.get(`${ routePrefix }${ path }`, routeHandler);
}
};
};
"debug" info “调试信息
// Output
// [start:dev] path, target, key -> /login, [object Object], getLogin
// [start:dev] here is the path! -> undefined
Ok, solved.好的,解决了。 I was passing the wrong args to the Reflect.getMetadata method.
我将错误的参数传递给 Reflect.getMetadata 方法。
It was它是
Reflect.getMetadata('path', target.prototype, key);
instead of代替
Reflect.getMetadata('path', target.prototype[key]); // Wrong!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.