嵌套指针数据

时间:2017-12-14 21:19:35

标签: javascript angularjs parse-platform parse-server

我使用Parse Server Javascript API调用下面的Parse查询。我有一个名为设备的类,它有一个 Device_Type 的指针对象,后者又有一个指向制造商的指针。

我可以在inventoryItem中成功显示deviceID,但是对于制造商来说是未定义的,尽管JSON网络中有相关数据返回。

$scope.getDeviceItems = function () {
    var query = new Parse.Query("Device");
    query.include("Device_Type");
    query.include("Device_Type.Manufacturer");
        query.find({
            success: function (results) {
                $scope.deviceItems = [];
                for (i = 0; i < results.length; i++) {
                    var d = results[i].get("Device_Type");
                    var m = results[i].get("Device_Type.Manufacturer");
                    var inventoryItem = {
                        deviceID: d.get("Device_ID"),
                        manufacturer: m.get("Manufacturer_Name")
                    }
                };
            };
        };
    };

如何正确获取嵌套指针数据到制造商?

这是一个示例JSON对象:

{
    "results": [{
        "objectId": "q3D2myHG3e",
        "createdAt": "2017-07-30T22:28:05.035Z",
        "updatedAt": "2017-09-19T20:40:05.580Z",
        "Device_Type": {
            "objectId": "Ksej3F2sdp5",
            "Device_Name": "Device 1",
            "createdAt": "2017-07-30T21:57:58.638Z",
            "updatedAt": "2017-08-08T20:20:14.397Z",
            "Manufacturer": {
                "objectId": "3NahYBNIiC",
                "Manufacturer_Name": "ABC Ltd",
                "createdAt": "2017-07-30T21:56:40.557Z",
                "updatedAt": "2017-07-30T22:13:07.425Z",
                "Devices": {
                    "__type": "Relation",
                    "className": "DeviceType"
                },
                "__type": "Object",
                "className": "DeviceManufacturer"
            },
            "__type": "Object",
            "className": "DeviceType"
        },
        ...
    }]
}

1 个答案:

答案 0 :(得分:1)

$scope.getDeviceItems = function () {
    var query = new Parse.Query("Device");
    query.include("Device_Type");
    query.include("Device_Type.Manufacturer");
        query.find({
            success: function (results) {
                $scope.deviceItems = [];
                for (i = 0; i < results.length; i++) {
                    var curResult = results[i];
                    var d = curResult.Device_Type;
                    var m = curResult.Device_Type.Manufacturer;
                    var inventoryItem = {
                        deviceID: d.Device_ID
                        manufacturer: m.Manufacturer_Name
                    }
                };
            };
        };
    };

不确定为什么地图正在被使用如上所示的常规JS对象会更容易(假设使用JS对象解析查找函数解析JSON而不是将其转换为Map对象)

使用你需要调用的地图,我可以告诉他解决“更深层”的密钥。

$scope.getDeviceItems = function () {
    var query = new Parse.Query("Device");
    query.include("Device_Type");
    query.include("Device_Type.Manufacturer");
        query.find({
            success: function (results) {
                $scope.deviceItems = [];
                for (i = 0; i < results.length; i++) {
                    var curResult = results[i];
                    var d = curResult.get('Device_Type');
                    var m = curResult.get('Device_Type').get('Manufacturer'); // or d.get('Manufacturer')
                    var inventoryItem = {
                        deviceID: d.get('Device_ID')
                        manufacturer: m.get('Manufacturer_Name')
                    }
                };
            };
        };
    };

var testObj = new Map();
testObj.set('a', new Map());
testObj.get('a').set('b', 4);

console.log(testObj.get('a').get('b'))