Start a new topic

how to get collection data in business logic


I have users Collection. In which i have some fields like phone_number, name and i am playing with phone number.

What i need to do. I am getting phone number(my primary key is phone number) from user and when user press save i need to check if number is available then update with available record and if not then add new record.

I tried following code but i am not able to fetch record in business logic.

Here is my presave() code.

function onPreSave(request, response, modules){

var collectionAccess = modules.collectionAccess

, userCollection = collectionAccess.collection('Users')

, utils = modules.utils

, logger = modules.logger;

//"name = " + " number = "+request.body.phonenumer +" userid = "+request.body.userid);

userCollection.find({"phonenumer":request.body.phonenumer}),(function(err, docs) {


{"error1 "+err);

}"docs "+docs);

userCollection.update({}, function(err, result) {

isUpdate = true;



logger.infor("error2 "+err);


logger.infor("result "+result);




var db = modules.collectionAccess,

objectToSave = { name: ,phonenumer: request.body.phonenumer ,userid: request.body.userid};

db.collection('Users').save(objectToSave, function(err, objectThatWasSaved) {

if (err) {

// do some error reporting here

} else {

// Hooray! It worked

// !! Make sure to call res.complete or res.continue

// !! to tell Kinvey you are done processing. Check

// !! the docs I linked to for the details






Have you seen this part of the documentation? It describes fetching data from collections in BL.

Looking closely at your first code example, I see a bit of an issue. When you first use the find method, you seem to have some issues with parenthesis and therefore it looks like you not actually passing the three arguments correctly. Look carefully after you specify the phonenumber in find, and try rewriting from scratch.
@ohmzTech is there any example to fetch/update/store data in collection using collection hooks.

I am trying to get all data from collection but still it's not working..

progressCollection.find().toArray(function(err, docs) {

var logger = modules.logger;"server response"+docs.length)


See all my collections require `preSave()` and `postSave.()` When my client send data i need to put this data by doing some formatting or appending or deleting in `preSave()` and once i saved data then in `postSave()` i need to send push notification.

See i am trying hard but i am not getting solutions. Kinvey is good but also new so less discussions are available.

Kindly, Let me know if need any more info. Thanks in Advance.
@OhmzTech Hi,

Thanks for your response, What i need to do. I need to write one collection hook for one of my collection "Progress" . In preSave() i need to get data from completed_task and want to append some data in to it and then need to save it. But first i don't understand how to fetch data from collection and update specific challenge..
Can you explain a bit more here? When you say:

> I tried following code but i am not able to fetch record in business logic.

Does this mean you are having problems with the updating portion, creating a new entry, or both? I'm guessing you are having issues updating, since you said you cannot fetch the record.

When you say primary key is phone number, what exactly do you mean? Do you really mean this is a unique key? Do you get any logger output you can share with us to helpful debugging?

Login or Signup to post a comment