Parse.com无法输出正确的JSON

时间:2014-09-02 21:54:22

标签: node.js parse-platform

假设我在Parse中有两个类(表):

--- TABLE 1: Navigation -------------------
objectId
Name
Url
-------------------------------------------

--- TABLE 2: Items ------------------------
objectId
Navigation (pointer to Navigation class)
Name
Price
-------------------------------------------

我想要的只是一些JSON输出,如下所示:

[
{
name: "Home",
url: "home"
},
{
name: "Menu",
url: "menu",
items: [{
    name: "French Fries",
    price: 3.00
    },
    {
    name: "Hamburger",
    price: 4.00
    },
    {
    name: "Cheeseburger",
    price: 5.00
    }]
},
{
name: "Dinner Menu",
url: "dinner-menu",
items: [{
    name: "Beer",
    price: 3.00
    },
    {
    name: "Wine (glass)",
    price: 9.00
    },
    {
    name: "Liquor Premium",
    price: 8.00
    }]
},
{
name: "Like and Follow Us",
url: "social-media"
}
]

我已经尝试了所有功能,在其中使用嵌套查询进行查询以循环items。我已经尝试查看Parse.Promises的文档,我认为这是问题的一部分,并且无法得到如上所示的结果。帮助!

我的破码:

var mainNav = [];
var navigationQuery = new Parse.Query(Parse.Object.extend("Navigation"));
navigationQuery.ascending("SortOrder").find().then(function(navItems) {

    _.each(navItems, function(navItem) {

        var newObj = {};
        newObj.Name = navItem.get("Name");
        newObj.Url = navItem.get("Url");
        newObj.Module = navItem.get("Module");
        newObj.SortOrder = navItem.get("SortOrder");

        var itemQuery = new Parse.Query(Parse.Object.extend("ListItems")).equalTo('Navigation', navItem).ascending('SortOrder').find(function(items) {
            newObj.Source = items; // not getting added to object!
        });
        mainNav.push(newObj);

    });


}).then(function(items) {

    res.send(mainNav);

});

1 个答案:

答案 0 :(得分:0)

find() over itemQuery是异步运行的,但是您将查询结果添加到完成块之外的mainNav,因此此代码不太可能按照您期望的方式运行。您可能希望在find()块中使用Promises来累积所有Source项,然后再将它们添加到mainNav。