質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

3回答

556閲覧

JavaScriptのfetch文について

gouehara

総合スコア12

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

1クリップ

投稿2019/03/15 14:40

編集2019/03/17 23:15

ここであるfetchを使った文を書きます。

function getLocation(city) { fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/search/?query=${city}`) .then(result => { return result.json(); }) .then(data => { console.log(data[0]); }) } function getWeather(woeId, year, mo, day) { fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/${woeId}/${year}/${mo}/${day}/`) .then(result => { return result.json(); }) .then(data => { // const today = data.consolidated_weather[0]; console.log(`Temperatures in Tokyo stay between ${data[0].min_temp} and ${data[0].max_temp}.`); }) .catch(error => console.log(error)); } getLocation('tok');//オブジェクトを表記 getWeather(1118370, 2019, 3, 15);//オブジェクトのwoeidプロパティの数値を最初のアーギュメントに代入。

これは東京の気温を表記する文ですが、これを前置きとして、ここで質問ですが。getWeather(1118370, この部分を getWeather(tokyo.woeid, みたいな感じに持って行きたいのですが、どこをどのようにすれば良いのでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gogojp

2019/03/15 15:03

ソースコードはcodeタグを用いて記述してください。 編集よろしくお願いします。
guest

回答3

0

ベストアンサー

async await で書くならこうでしょうか。(動作未確認)

js

1function getLocation(city) { 2 return fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/search/?query=${city}`) 3 .then(result => { 4 return result.json(); 5 }) 6} 7 8function getWeather(woeId, year, mo, day) { 9 fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/${woeId}/${year}/${mo}/${day}/`) 10 .then(result => { 11 return result.json(); 12 }) 13 .then(data => { 14 // const today = data.consolidated_weather[0]; 15 console.log(`Temperatures in Tokyo stay between ${data[0].min_temp} and ${data[0].max_temp}.`); 16 17 }) 18 .catch(error => console.log(error)); 19} 20 21async function test(){ 22 const tokyo = await getLocation('tok');//オブジェクトを表記 23 getWeather(tokyo.woeid, 2019, 3, 15);//オブジェクトのwoeidプロパティの数値を最初のアーギュメントに代入。 24}

then でつなぐならこうでしょうか。(動作未確認)

js

1function getLocation(city) { 2 return fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/search/?query=${city}`) 3 .then(result => { 4 return result.json(); 5 }) 6} 7 8function getWeather(woeId, year, mo, day) { 9 fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/${woeId}/${year}/${mo}/${day}/`) 10 .then(result => { 11 return result.json(); 12 }) 13 .then(data => { 14 // const today = data.consolidated_weather[0]; 15 console.log(`Temperatures in Tokyo stay between ${data[0].min_temp} and ${data[0].max_temp}.`); 16 17 }) 18 .catch(error => console.log(error)); 19} 20 21getLocation('tok').then(data=>{ 22 tokyo = data 23 getWeather(tokyo.woeid, 2019, 3, 15);//オブジェクトのwoeidプロパティの数値を最初のアーギュメントに代入。 24})

投稿2019/03/16 00:42

編集2019/03/16 00:43
Lhankor_Mhy

総合スコア36117

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gouehara

2019/03/17 13:55

ありがとうございます。 なんとか成功しました。 ので、追記したいと思います。
gouehara

2019/03/18 03:10

async await では、動作を確認できました。 実際にやったのはこんな感じです。 async function test(){ const tokyo = await getLocation('tok'); getWeather(tokyo[0].woeid, 2019, 3, 15); // tokyo variableはアレイであるため、その最初の要素が東京都を示している。 }
gouehara

2019/03/18 03:16

私のやりたかった事はこの async await で表現出来てると思います。 ありがとうございました。
Lhankor_Mhy

2019/03/18 03:28

ああ、配列で返ってくるのですね。 APIの仕様をググって見てたのですが、そこは見落としていました。ご解決されて何よりです。
guest

0

function getLocation(city = 'tok') { fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/search/?query=${city}`) .then(result => { return result.json(); }) .then(data => { tokyoId = data[0].woeid; return tokyoId; }) .then(id => { getWeather(id, 2019, 3, 17); }) } function getWeather(woeId, year, mo, day) { fetch(`https://cors-anywhere.herokuapp.com/www.metaweather.com/api/location/${woeId}/${year}/${mo}/${day}/`) .then(result => { return result.json(); }) .then(data => { console.log(`Temperatures in Tokyo stay between ${data[0].min_temp} and ${data[0].max_temp}.`); }) .catch(error => console.log(error)); } getLocation();

投稿2019/03/17 14:12

gouehara

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

getWeather(1118370, この部分を getWeather(tokyo.woeid, みたいな感じに

こういうこと?

js

1var tokyo = { woeid : 1118370 }; 2getWeather( tokyo.woeid, /*略*/ );

投稿2019/03/15 19:53

kei344

総合スコア69407

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gouehara

2019/03/16 13:01

なるべく、プロパティバリューの数値1118370を明記しないで getWeather(data[0].woeid, /* 略 */); というような感じに出来ないでしょうか。 出来るとしたら、どこを変えれば良いか教えて欲しいです。
kei344

2019/03/16 13:09

Lhankor_Mhyさんの回答をご覧ください。
退会済みユーザー

退会済みユーザー

2019/03/16 13:13

この返しは脳内で考えた値を自動的に設定したいのかな?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問