Merge pull request #1312 from ParsePlatform/flovilmart.includesArray
Fixes bug related to include in queries
This commit is contained in:
@@ -1422,6 +1422,78 @@ describe('Parse.Query testing', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('properly includes array', (done) => {
|
||||||
|
let objects = [];
|
||||||
|
let total = 0;
|
||||||
|
while(objects.length != 5) {
|
||||||
|
let object = new Parse.Object('AnObject');
|
||||||
|
object.set('key', objects.length);
|
||||||
|
total += objects.length;
|
||||||
|
objects.push(object);
|
||||||
|
}
|
||||||
|
Parse.Object.saveAll(objects).then(() => {
|
||||||
|
let object = new Parse.Object("AContainer");
|
||||||
|
object.set('objects', objects);
|
||||||
|
return object.save();
|
||||||
|
}).then(() => {
|
||||||
|
let query = new Parse.Query('AContainer');
|
||||||
|
query.include('objects');
|
||||||
|
return query.find()
|
||||||
|
}).then((results) => {
|
||||||
|
expect(results.length).toBe(1);
|
||||||
|
let res = results[0];
|
||||||
|
let objects = res.get('objects');
|
||||||
|
expect(objects.length).toBe(5);
|
||||||
|
objects.forEach((object) => {
|
||||||
|
total -= object.get('key');
|
||||||
|
});
|
||||||
|
expect(total).toBe(0);
|
||||||
|
done()
|
||||||
|
}, () => {
|
||||||
|
fail('should not fail');
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('properly includes array of mixed objects', (done) => {
|
||||||
|
let objects = [];
|
||||||
|
let total = 0;
|
||||||
|
while(objects.length != 5) {
|
||||||
|
let object = new Parse.Object('AnObject');
|
||||||
|
object.set('key', objects.length);
|
||||||
|
total += objects.length;
|
||||||
|
objects.push(object);
|
||||||
|
}
|
||||||
|
while(objects.length != 10) {
|
||||||
|
let object = new Parse.Object('AnotherObject');
|
||||||
|
object.set('key', objects.length);
|
||||||
|
total += objects.length;
|
||||||
|
objects.push(object);
|
||||||
|
}
|
||||||
|
Parse.Object.saveAll(objects).then(() => {
|
||||||
|
let object = new Parse.Object("AContainer");
|
||||||
|
object.set('objects', objects);
|
||||||
|
return object.save();
|
||||||
|
}).then(() => {
|
||||||
|
let query = new Parse.Query('AContainer');
|
||||||
|
query.include('objects');
|
||||||
|
return query.find()
|
||||||
|
}).then((results) => {
|
||||||
|
expect(results.length).toBe(1);
|
||||||
|
let res = results[0];
|
||||||
|
let objects = res.get('objects');
|
||||||
|
expect(objects.length).toBe(10);
|
||||||
|
objects.forEach((object) => {
|
||||||
|
total -= object.get('key');
|
||||||
|
});
|
||||||
|
expect(total).toBe(0);
|
||||||
|
done()
|
||||||
|
}, (err) => {
|
||||||
|
fail('should not fail');
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it("result object creation uses current extension", function(done) {
|
it("result object creation uses current extension", function(done) {
|
||||||
var ParentObject = Parse.Object.extend({ className: "ParentObject" });
|
var ParentObject = Parse.Object.extend({ className: "ParentObject" });
|
||||||
// Add a foo() method to ChildObject.
|
// Add a foo() method to ChildObject.
|
||||||
|
|||||||
@@ -449,39 +449,42 @@ function includePath(config, auth, response, path) {
|
|||||||
if (pointers.length == 0) {
|
if (pointers.length == 0) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
let pointersHash = {};
|
||||||
var className = null;
|
var className = null;
|
||||||
var objectIds = {};
|
var objectIds = {};
|
||||||
for (var pointer of pointers) {
|
for (var pointer of pointers) {
|
||||||
if (className === null) {
|
let className = pointer.className;
|
||||||
className = pointer.className;
|
// only include the good pointers
|
||||||
} else {
|
if (className) {
|
||||||
if (className != pointer.className) {
|
pointersHash[className] = pointersHash[className] || [];
|
||||||
throw new Parse.Error(Parse.Error.INVALID_JSON,
|
pointersHash[className].push(pointer.objectId);
|
||||||
'inconsistent type data for include');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
objectIds[pointer.objectId] = true;
|
|
||||||
}
|
|
||||||
if (!className) {
|
|
||||||
throw new Parse.Error(Parse.Error.INVALID_JSON,
|
|
||||||
'bad pointers');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the objects for all these object ids
|
let queryPromises = Object.keys(pointersHash).map((className) => {
|
||||||
var where = {'objectId': {'$in': Object.keys(objectIds)}};
|
var where = {'objectId': {'$in': pointersHash[className]}};
|
||||||
var query = new RestQuery(config, auth, className, where);
|
var query = new RestQuery(config, auth, className, where);
|
||||||
return query.execute().then((includeResponse) => {
|
return query.execute().then((results) => {
|
||||||
var replace = {};
|
results.className = className;
|
||||||
|
return Promise.resolve(results);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Get the objects for all these object ids
|
||||||
|
return Promise.all(queryPromises).then((responses) => {
|
||||||
|
var replace = responses.reduce((replace, includeResponse) => {
|
||||||
for (var obj of includeResponse.results) {
|
for (var obj of includeResponse.results) {
|
||||||
obj.__type = 'Object';
|
obj.__type = 'Object';
|
||||||
obj.className = className;
|
obj.className = includeResponse.className;
|
||||||
|
|
||||||
if(className == "_User"){
|
if(className == "_User"){
|
||||||
delete obj.sessionToken;
|
delete obj.sessionToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
replace[obj.objectId] = obj;
|
replace[obj.objectId] = obj;
|
||||||
}
|
}
|
||||||
|
return replace;
|
||||||
|
}, {})
|
||||||
|
|
||||||
var resp = {
|
var resp = {
|
||||||
results: replacePointers(response.results, path, replace)
|
results: replacePointers(response.results, path, replace)
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user