修改noJson的实现逻辑

This commit is contained in:
zhl 2021-04-23 18:04:16 +08:00
parent 6c274bb7c6
commit 9a017e7606
3 changed files with 20 additions and 7 deletions

View File

@ -1,18 +1,32 @@
import 'reflect-metadata'
import { singleton } from './singleton'
const noJsonMetadataKey = ('noJsonSet')
@singleton
export class NoJsonClass {
private noJsonPropSet: Set<string> = 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)
}

View File

@ -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]
}
}

View File

@ -13,7 +13,6 @@ import { noJson } from '../../decorators/nojson'
class PuzzleClass extends BaseModule {
@prop()
public question: string
@prop()
public a1: string
@prop()