无法获取对象值并在 class 中定义常量和静态变量
Unable to fetch object value and define constant and static variable in class
我有以下代码:
'use strict';
const Request = require('request');
class CryptoKetHandlers {
static async coins(ctx) {
try {
var CONTENT_TYPE = 'application/json';
console.log(`## Hitting cryptoket coins api for coins list ...`);
var res = await Request.get({
url: 'https://api.cryptoket.io/info/currencies',
headers: {
'Content-Type': CONTENT_TYPE,
}
}, (err, res, body) => {
//console.log(`Inside response ... res: ${res} err: ${err} body: ${body}`);
if (err) {
console.log(`#### ERROR :: API RESP :: CryptoKetHandlers :: coins :: exception :: ${err}`);
ctx.throw(500);
} else {
let res = {};
res.body = body;
res.status = 200;
return res;
}
});
} catch (ex) {
console.log(`#### ERROR :: CryptoKetHandlers :: coins :: exception :: ${ex}`);
ctx.throw(500);
}
console.log(`######### Final Response :: status - ${res.status} body - ${res.body} res :: ${res}`);
ctx.body = res;
ctx.status = 200;
}
}
module.exports = CryptoKetHandlers;
我来自 Java 背景并且是 koa 和 nodejs 的新手所以想知道我是否可以实现以下目标:
- 为什么 res.status 和 res.body 总是 return 未定义?
- 如何将 CONTENT_TYPE 声明为 class 变量应该是 const & static?
想要实现的是:
Return 所有这些来自 Request.get 和
的 err、res、body 参数
在其他地方处理它(在另一个 class 函数中)。
- 将所有可重用变量设为 class 级别,这样就不需要再定义了。
- 如果您有一些建议可以使我的代码更健壮和更简洁。
我通过谷歌搜索尝试了各种方法但没有成功所以在这里问。
提前致谢。
- Why res.status and res.body always return undefined?
首先,Request.get()
应该 return 一个 Promise
工作,然后 await
工作。
The await expression causes async function execution to pause until a Promise is resolved, that is fulfilled or rejected, and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise.
其次,您不小心将 return 值 res
替换为 (err, res, body) => {}
中的另一个 res
。相反,我们可以 return return 值作为普通对象。
进行这些更改,
'use strict';
let request = require('request');
const CONTENT_TYPE = 'application/json';
function makeRequest() {
return new Promise((resolve, reject) => {
request.get({
url: 'https://api.cryptoket.io/info/currencies',
headers: {
'Content-Type': CONTENT_TYPE,
}
}, (err, res, body) => {
if (err) {
console.log(`#### ERROR :: API RESP :: CryptoKetHandlers :: coins :: exception :: ${err}`);
return reject(err);
}
// return as object
return resolve({
body: body,
status: 200
});
});
});
}
class CryptoKetHandlers {
static async coins(ctx) {
try {
console.log(`## Hitting cryptoket coins api for coins list ...`);
let res = await makeRequest();
console.log(`######### Final Response :: status - ${res.status} body - ${res.body} res :: ${res}`);
} catch (ex) {
console.log(`#### ERROR :: CryptoKetHandlers :: coins :: exception :: ${ex}`);
}
}
}
因为我用 let
替换了 var
。我已将最终日志移至 try
块内。
- How can I declare CONTENT_TYPE as a class variable should be const & static?
此提案正在进行中。您可以查看 ES6 class variable alternatives 了解更多信息。
我有以下代码:
'use strict';
const Request = require('request');
class CryptoKetHandlers {
static async coins(ctx) {
try {
var CONTENT_TYPE = 'application/json';
console.log(`## Hitting cryptoket coins api for coins list ...`);
var res = await Request.get({
url: 'https://api.cryptoket.io/info/currencies',
headers: {
'Content-Type': CONTENT_TYPE,
}
}, (err, res, body) => {
//console.log(`Inside response ... res: ${res} err: ${err} body: ${body}`);
if (err) {
console.log(`#### ERROR :: API RESP :: CryptoKetHandlers :: coins :: exception :: ${err}`);
ctx.throw(500);
} else {
let res = {};
res.body = body;
res.status = 200;
return res;
}
});
} catch (ex) {
console.log(`#### ERROR :: CryptoKetHandlers :: coins :: exception :: ${ex}`);
ctx.throw(500);
}
console.log(`######### Final Response :: status - ${res.status} body - ${res.body} res :: ${res}`);
ctx.body = res;
ctx.status = 200;
}
}
module.exports = CryptoKetHandlers;
我来自 Java 背景并且是 koa 和 nodejs 的新手所以想知道我是否可以实现以下目标:
- 为什么 res.status 和 res.body 总是 return 未定义?
- 如何将 CONTENT_TYPE 声明为 class 变量应该是 const & static?
想要实现的是:
Return 所有这些来自 Request.get 和
的 err、res、body 参数 在其他地方处理它(在另一个 class 函数中)。- 将所有可重用变量设为 class 级别,这样就不需要再定义了。
- 如果您有一些建议可以使我的代码更健壮和更简洁。
我通过谷歌搜索尝试了各种方法但没有成功所以在这里问。
提前致谢。
- Why res.status and res.body always return undefined?
首先,Request.get()
应该 return 一个 Promise
工作,然后 await
工作。
The await expression causes async function execution to pause until a Promise is resolved, that is fulfilled or rejected, and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise.
其次,您不小心将 return 值 res
替换为 (err, res, body) => {}
中的另一个 res
。相反,我们可以 return return 值作为普通对象。
进行这些更改,
'use strict';
let request = require('request');
const CONTENT_TYPE = 'application/json';
function makeRequest() {
return new Promise((resolve, reject) => {
request.get({
url: 'https://api.cryptoket.io/info/currencies',
headers: {
'Content-Type': CONTENT_TYPE,
}
}, (err, res, body) => {
if (err) {
console.log(`#### ERROR :: API RESP :: CryptoKetHandlers :: coins :: exception :: ${err}`);
return reject(err);
}
// return as object
return resolve({
body: body,
status: 200
});
});
});
}
class CryptoKetHandlers {
static async coins(ctx) {
try {
console.log(`## Hitting cryptoket coins api for coins list ...`);
let res = await makeRequest();
console.log(`######### Final Response :: status - ${res.status} body - ${res.body} res :: ${res}`);
} catch (ex) {
console.log(`#### ERROR :: CryptoKetHandlers :: coins :: exception :: ${ex}`);
}
}
}
因为我用 let
替换了 var
。我已将最终日志移至 try
块内。
- How can I declare CONTENT_TYPE as a class variable should be const & static?
此提案正在进行中。您可以查看 ES6 class variable alternatives 了解更多信息。