diff --git a/src/decorators/nojson.ts b/src/decorators/nojson.ts index 8d49fb1..e4c5518 100644 --- a/src/decorators/nojson.ts +++ b/src/decorators/nojson.ts @@ -1,18 +1,32 @@ import 'reflect-metadata' +import { singleton } from './singleton' const noJsonMetadataKey = ('noJsonSet') +@singleton +export class NoJsonClass { + private noJsonPropSet: Set = new Set() + + public addKey(className: string, propertyKey: string) { + this.noJsonPropSet.add(className+'_'+propertyKey) + } + + public checkExist(className: string, propertyKey: string) { + return this.noJsonPropSet.has(className+'_'+propertyKey) + } +} /** - * 这方法无效 * 在不需要toJson方法输出的字段上加上 @noJson * @return {{(target: Function): void, (target: Object, propertyKey: (string | symbol)): void}} */ export function noJson() { - return Reflect.metadata(noJsonMetadataKey, !0) + // return Reflect.metadata(noJsonMetadataKey, !0) + return function(target: Object, propertyKey: string) { + console.log(target) + new NoJsonClass().addKey(target.constructor.name, propertyKey) + } } export function checkJson(target: any, propertyKey: string) { - let val = Reflect.getMetadata(noJsonMetadataKey, target, propertyKey) - let s = Reflect.getMetadata('typegoose:properties', target, propertyKey) - return !val + return !new NoJsonClass().checkExist(target.constructor.modelName, propertyKey) } diff --git a/src/models/Base.ts b/src/models/Base.ts index e57640b..58a11a0 100644 --- a/src/models/Base.ts +++ b/src/models/Base.ts @@ -28,7 +28,7 @@ export abstract class BaseModule extends FindOrCreate { let result: any = {} // @ts-ignore for (let key in this._doc) { - if (jsonExcludeKeys.indexOf(key) == -1) { + if (checkJson(this, key+'' ) && jsonExcludeKeys.indexOf(key) == -1) { result[key] = this[key] } } diff --git a/src/models/content/Puzzle.ts b/src/models/content/Puzzle.ts index 6d21ab4..14eb80e 100644 --- a/src/models/content/Puzzle.ts +++ b/src/models/content/Puzzle.ts @@ -13,7 +13,6 @@ import { noJson } from '../../decorators/nojson' class PuzzleClass extends BaseModule { @prop() public question: string - @prop() public a1: string @prop()