gpt4 book ai didi

javascript - 解析数据库 JavaScript 和 PhoneGap

转载 作者:行者123 更新时间:2023-12-03 08:09:53 25 4
gpt4 key购买 nike

我一直在开发一个餐厅应用程序,该应用程序应该从在线数据库获取菜单。

这就是我目前“手动”填充菜单的方式:

Controller.html

$scope.menu = [{
name: 'Espresso',
price: 27,
qty: 1,
desc: "One shot of espresso prepared with 7 grams of ground coffee in a single portafilter. The shot should be 1 ounce of liquid. You have two choices with espresso: ristretto, a very short or “restrained” shot, brewed at less than 2/3 of a demitasse, or luongo, a long pull of espresso brewed so the liquid should be more than 2/3 of a demitasse.",
img: "img/espresso.png",
active: false,
sizes: [{name: "Small", price: 0, active:false},
{name: "Medium", price: 5, active:false},
{name: "Large", price: 10, active:false}],
flavors: [{name: 'Vanilla', price: 8, active: false},
{name: 'Almond', price: 8, active: false},
{name: 'Hazelnut', price: 8, active: false},
{name: 'Caramel', price: 8, active: false}]
}];

但是,我似乎无法使用 Parse 来填充此内容,我将如何使用如下查询来实现此目的(这是一个有效的查询)。

Index.html

<script type="text/javascript">

Parse.initialize("vGoJDvwwfZBiUFcwfkee7M5vCqL7lLxCgKIFJXDc", "6VRlos6qppaek1uDPPLqpHtmB3fHefOJMqYJNxj9");

var DrinkMenu = Parse.Object.extend("DrinkMenu");
var query = new Parse.Query(DrinkMenu);
query.find({
success: function(results) {
alert("Successfully retrieved " + results.length + " items.");
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
alert(object.id + ' - ' + object.get('name'));
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});

</script>

您可以注意到我可以获得每个项目所需的变量,在本例中是第一个结果的名称,我将其显示在警报中。

感谢任何帮助!

最佳答案

在 Parse.initialize 之后,创建一个变量,如下所示:

var arrMenu = [];

然后更改行alert(object.id + ' - ' + object.get('name'));至

arrMenu.push({
name: object.get('name'),
price: object.get('price'),
qty: object.get('qty'),
desc: object.get('desc'),
img: object.get('img'),
active: object.get('active'),
sizes: object.get('sizes'),
flavors: object.get('flavor')
});

我假设您使用您提到的结构将信息存储在 Parse Collection 中。如果有不同,请告诉我。

并且,在括号结束 for 之后,添加:

$scope.menu = arrMenu;

希望对您有帮助!

关于javascript - 解析数据库 JavaScript 和 PhoneGap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191907/

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