Start a new topic

Unable to set properties for object in collection.find

Hi,



I am using below code to find records from some collection.



var tests = {};

testCollection.find({testid: testNumber}, { sort:{timestamp: 1 },

function( err, results) {



if(err) {

logger.error("Unable to proceed");

}

for(var i = 0; i
tests[i] = results[i].name;

}

});



logger.info("Available Tests");

logger.info(tests);



So I am always getting tests printed as {};



Why the properties are not being set to tests in for loop ?

Hi @Michael I tried to print response use Logger then i am able to see the response in collection hook but i am getting null on my client.



challengeCollection.find({challangeid: jsonObject.challangeid,

group_id: jsonObject.group_id},

function(err, result) {

if(err) {

logger.info("Error while fetching challenge data "+err);

}

startDate = new Date(result[0].startdate);

challengeDurationInDays = result[0].duration;

});



groupUserMappingCollection.find({group_id: jsonObject.group_id},

function(err, result) {

if(err) {

logger.error("Error while fetching data "+err);

}



logger.info("Results found "+result.length);

for(var i = 0; i
var userid = result[i].userid;

response.body.userList[userid] = {};

}

var test = "1";

if( Object.keys(response.body.userList).length > 0 ) {



progressCollection.find({challangeid: jsonObject.challangeid,

group_id: jsonObject.group_id},

{ sort:{timestamp: 1}, awaitdata: true },

function(err, result) {

if(err) {

logger.error("Error while fetching records "+err);

}

for( var i = 0; i


if(result[i]) {

var progressObj = result[i].progress;

var currentDate = new Date(result[i].date);

var dateDiffInDays = getDateDifferenceInDays(currentDate, startDate);

if(dateDiffInDays
var day = "Day"+(dateDiffInDays+1);

for(var j = 0; j
response.body.userList[progressObj[j].userid]["phno"] = progressObj[j].phonenumer;

response.body.userList[progressObj[j].userid]["name"] = progressObj[j].username;

k = j+1;

if(k==progressObj.length){

logger.error(response.body); // Here i am able to see response

response.complete(201); // also use response.complete() or compete(201);

}

}

}

}

}

});

}

}

);



But when i am trying to get data in my app the i am getting null.



protected void getAllProgress() {

Query progressQuery = mKinveyClient.query();

progressQuery.equals("group_id",groupId);

progressQuery.equals("challangeid",challangeId);

//progressQuery.equals("startdate",cha);



AsyncAppData progressEvents = mKinveyClient.appData("ProgressSample", ProgressSample.class);

progressEvents.get(progressQuery,new KinveyListCallback() {

@Override

public void onSuccess(ProgressSample[] results) {

Log.i("PROGRESS", "Success while fetching progress result and results length :- "+results);

//List progressList = Arrays.asList(results);

//Log.i("PROGRESS", "iS THERE ANY ITEM IN LIST"+progressList);



}

@Override

public void onFailure(Throwable error) {

Log.e("PROGRESS", "failed to fetchByFilterCriteria", error);

}

});

}



I don't understand if i am able to see response in collection hook and and sending it to client then why i am getting null.



Please give me any reference or hint
If you want to send it back to the client, then yes, you need to set response.body and response.complete in the callback itself.
I got your point of moving logger statement into find function itself, but actually I was asking about whether I need to set data to response.body in find itself ? so that I can receive it ?

Try the below:



`

var tests = {};

testCollection.find({testid: testNumber}, {sort: {timestamp: 1}, function(err, results) {

if (err) {

logger.error("Unable to proceed");

}

for (var i = 0; i
tests[i] = results[i].name;

}

logger.info(tests);

});

`

I tried with awaitdata but still its not seems to be working.... and do I need to set it to response in find itself ?
testCollection.find() is an asynchronous call to the database. In your example, the find method is executed asynchronously, and then the logger methods are immediately executed before the find callback is invoked. If you move the logger statements at the end of this code to within the callback (after the for loop) it should resolve this issue.
Login or Signup to post a comment