gpt4 book ai didi

javascript - 为什么我的 (this.ingredients) 未定义?

转载 作者:行者123 更新时间:2023-12-01 00:33:30 30 4
gpt4 key购买 nike

我试图将成分记录到控制台,但它总是未定义

export default class Recipe {
constructor(ID) {
this.ID = ID;
}

getRicepeById () {
const res = await fetch(`https://www.food2fork.com/api/get?key=${key}&rId=${this.ID}`);
this.title = res.data.recipe.title;
this.image = res.data.recipe.image_url;
this.author = res.data.recipe.publisher;
this.ingredients = res.data.recipe.ingredients;
//this.ingredients = [1, 2, 3, 4, 4, 23, 2];
}

calculateTime() {
const totalIngredients = this.ingredients;
//here is my problem
console.log(totalIngredients)
}
}

最佳答案

getRicepeById 应该是 async,因为您在此方法的主体中使用了 await。另外,请确保您在 fetch 完成后调用 calculateTime

关于javascript - 为什么我的 (this.ingredients) 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58327333/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com