eggjs / eggjs/egg

Getter/Setter does not work with ES6 class in extend/context.ts

Open
#4,795 1 comment 0 reactions 0 assignees View on GitHub
Inactive
Dominant language
TypeScript
Stars
19k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

## What happens?
Context Getter/Setter does not work with ES6 class.

class Context {
constructor() {
}
/**
** - Return all operators of Sequelize
** @url https://demopark.github.io/sequelize-docs-Zh-CN/querying.html
**/
get Op() {
return this.app.Sequelize.Op;
}
get print() {
return this.body;
}
get realIP() {
return this.get['X-Real-IP'] || this.get['X-Forwarded-For'] || this.ip;
}
/**
** @param {*} value
** @example
** ctx.print = 'Hello'
** ctx.print = null
** ctx.print = {name: 'Hello'}
** @returns {void}
**/
set print(value) {

const body = {
data: value,
errorCode: 0,
msg: ERROR_CODE[0],
success: true
};

if (isPlainObject(value)) {

if (value.errorCode !== undefined && value.errorCode !== 0) {
body.errorCode = value.errorCode;
body.success = false;
body.msg = ERROR_CODE[body.errorCode] || 'error';
body.data = null;
}

body.msg = value.msg || body.msg;
body.errorMsg = value.errorMsg || body.msg;

// Delete duplicate defined fields
if (isPlainObject(body.data)) {
body.data = Object.assign({}, body.data);
delete body.data.errorCode;
delete body.data.msg;
delete body.data.success;
}
}

this.body = body;
}
}

// export default new Context();

module.exports = new Context();

**Steps to reproduce the behavior:**
1. The setter and getter do not run when I use ctx.print = <...something...>

**Expected behavior**
1. It should Run

## Context
- **Node Version**: 14
- **Egg Version**: 2.30
- **Platform**: NodeJS

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.