JavaScriptが新しく反応します。 IDを指定してサーバーからcustomer_nameを取得するコンポーネントからコールバックがあります。 フェッチが機能し、console.logがフルネームを正しく出力しますが、最後の.thenのcustomer_nameは設定されず、関数は空の文字列を返します。何故ですか?フェッチ:応答をフェッチして変数を設定し、関数から返す
// Gets the fullname of the customer from an id.
tj_customer_name(id) {
let customer_name = '';
fetch(`/customers/${id}.json`, {
headers: API_HEADERS,
credentials: 'same-origin'
})
.then((response) => {
if(response.ok) {
return response.json();
} else {
throw new Error('Server response wasn\'t OK');
}
})
.then((json) => {
customer_name = json.first_name.concat(' ').concat(json.last_name);
console.log(customer_name);
});
return customer_name;
}
あなたの関数を呼び出すコードで同じになりますあなたが主張した – Bergi