teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

ご回答分を修正

2018/10/24 09:36

投稿

tsu2626
tsu2626

スコア12

title CHANGED
File without changes
body CHANGED
@@ -66,4 +66,45 @@
66
66
  });
67
67
  ```
68
68
 
69
- よろしくおねがいします。
69
+ よろしくおねがいします。
70
+
71
+
72
+ # ご回答を頂いた部分を修正しました
73
+ ```js
74
+ var express = require('express');
75
+ var router = express.Router();
76
+
77
+ let https = require('https');
78
+
79
+ const XRP = "https://coincheck.com/api/rate/xrp_jpy";
80
+ var state = {};
81
+
82
+ https.get(XRP, (res) => {
83
+ var body = '';
84
+ res.setEncoding('utf8');
85
+ res.on('data', (chunk) => {
86
+ body += chunk;
87
+ });
88
+ res.on('end', (chunk) => {
89
+ // state.rate = JSON.stringify(body);
90
+ state.rate = JSON.parse(body);
91
+ console.log(`${state.rate}`);
92
+ })
93
+ }).on('error', (e) => {
94
+ console.log(e.message);
95
+ });
96
+
97
+ router.get('/', (req, res, next) => {
98
+ res.render('top', {
99
+ title: 'TOP',
100
+ rate: `現在のレートは${state.rate}円/XRP`
101
+ });
102
+ });
103
+
104
+ module.exports = router;
105
+ ```
106
+ 上記の通り、編集しました。
107
+ しかし結果は
108
+ コンソール→現在のレートは[object Object]円/XRP
109
+ ブラウザ→現在のレートは[object Object]円/XRP
110
+ でした。

2

試したことを追記

2018/10/24 09:36

投稿

tsu2626
tsu2626

スコア12

title CHANGED
File without changes
body CHANGED
@@ -55,4 +55,15 @@
55
55
  p #{rate}
56
56
  ```
57
57
 
58
+ # 試したこと
59
+ rateの中身を${res}に変更すると、[object Object]と表示されます。
60
+ ```js
61
+ router.get('/', (req, res, next) => {
62
+ res.render('top', {
63
+ title: 'TOP',
64
+ rate: `現在のレートは${res}円/XRP`
65
+ });
66
+ });
67
+ ```
68
+
58
69
  よろしくおねがいします。

1

コードを更新いたしました。 現状は「コンソールに表示はできるが、ブラウザ上ではundefined」になってしまうです。

2018/10/23 13:29

投稿

tsu2626
tsu2626

スコア12

title CHANGED
File without changes
body CHANGED
@@ -16,24 +16,28 @@
16
16
 
17
17
  const XRP = "https://coincheck.com/api/rate/xrp_jpy";
18
18
 
19
- https.get(XRP, function (res) {
19
+ https.get(XRP, (res) => {
20
- var body = '';
20
+ var body = '';
21
- res.setEncoding('utf8');
21
+ res.setEncoding('utf8');
22
- res.on('data', function (chunk) {
22
+ res.on('data', (chunk) => {
23
- body += chunk;
23
+ body += chunk;
24
+ });
25
+ res.on('end', (chunk) => {
26
+ // body の値を json としてパースしている
27
+ res = JSON.parse(body);
28
+ console.log(`現在のレートは${res.rate}円/XRP`);
29
+ })
30
+ }).on('error', (e) => {
31
+ console.log(e.message);
32
+ });
33
+
34
+ router.get('/', (req, res, next) => {
35
+ res.render('top', {
36
+ title: 'TOP',
37
+ rate: `現在のレートは${res.rate}円/XRP`
24
38
  });
25
- res.on('data', function (chunk) {
26
- // body の値を json としてパースしている
27
- res = JSON.parse(body);
28
- console.log(`現在のレートは${res.rate}円/XRP`);
29
- router.get('/', (req, res, next) => {
30
- res.render('top', {
31
- title: 'TOP',
32
- rate: `現在のレートは${res.rate}円/XRP`
33
- });
34
- });
35
- });
36
39
  });
40
+
37
41
  module.exports = router;
38
42
  ```
39
43
 
@@ -51,36 +55,4 @@
51
55
  p #{rate}
52
56
  ```
53
57
 
54
- ### 試したこと
55
- https.getの中にrouter.getに入れたり入れなかったりしてみました..
56
- ちなみに以下のコードだと、コンソール表示はできますがrate: `現在のレートは${res.rate}円/XRP`の部分はres.rate not difinedのようになります。
57
- ```js
58
- var express = require('express');
59
- var router = express.Router();
60
-
61
- let https = require('https');
62
-
63
- const XRP = "https://coincheck.com/api/rate/xrp_jpy";
64
-
65
- https.get(XRP, function (res) {
66
- var body = '';
67
- res.setEncoding('utf8');
68
- res.on('data', function (chunk) {
69
- body += chunk;
70
- });
71
- });
72
- res.on('data', function (chunk) {
73
- // body の値を json としてパースしている
74
- res = JSON.parse(body);
75
- console.log(`現在のレートは${res.rate}円/XRP`);
76
- router.get('/', (req, res, next) => {
77
- res.render('top', {
78
- title: 'TOP',
79
- rate: `現在のレートは${res.rate}円/XRP`
80
- });
81
- });
82
- });
83
- module.exports = router;
84
- ```
85
-
86
58
  よろしくおねがいします。