【发布时间】:2019-02-02 06:38:18
【问题描述】:
我有一个函数可以从 firestore 数据库中返回一组菜肴。 使用 console.log 我检查我要推送的菜的格式是否正确,然后推送它。
最后我 console.log 来检查数组是否一切正常。 这是我得到的:
https://image.noelshack.com/fichiers/2019/05/5/1549048418-arraypush.png
switch (type) {
case "Plats": {
this.nourritureCollection.ref.get().then(data => {
let platArray : Plat[] = [];
data.docs.forEach(doc => {
this.plat.name = doc.data().nourritureJson.name;
this.plat.price = doc.data().nourritureJson.price;
this.plat.ingredients = doc.data().nourritureJson.ingredients;
this.plat.type = doc.data().nourritureJson.type;
this.plat.availableQuantity = doc.data().nourritureJson.availableQuantity;
this.plat.isAvailableOffMenu = doc.data().nourritureJson.isAvailableOffMenu;
this.plat.imgUrl = doc.data().nourritureJson.imgUrl;
this.plat.temp = doc.data().nourritureJson.temp;
console.log(this.plat)
platArray.push(this.plat);
});
console.log(platArray)
return platArray;
});
break;
}...
plat 在我的服务组件中被实例化,我无法在我的函数中声明一个新的Plat()。
预期的结果是我的菜品阵列中的菜品应该是不同的。
【问题讨论】:
-
你每次推送同一个对象。
-
你不断地只改变一个对象
this.plat。在 forEach 中使用let plat:) -
是的,我不知道它为什么会这样。我虽然它会用新属性替换旧属性。我之前尝试创建一个新对象,但由于一些阻塞的范围而无法奇怪:/
标签: node.js angular ionic-framework