As of April 12th, you must go to Progress SupportLink to create new support cases or to access existing cases. Please, bookmark the SupportLink URL and use the new portal to contact the support team.
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.
> 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?
s
sandip_armal
said
about 9 years ago
@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..
s
sandip_armal
said
about 9 years ago
@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..
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.
O
OhmzTech
said
about 9 years ago
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.
sandip_armal
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;
//logger.info("name = "+request.body.name + " number = "+request.body.phonenumer +" userid = "+request.body.userid);
userCollection.find({"phonenumer":request.body.phonenumer}),(function(err, docs) {
if(err)
{
logger.info("error1 "+err);
}
logger.info("docs "+docs);
userCollection.update({name:request.body.name}, function(err, result) {
isUpdate = true;
if(err)
{
logger.infor("error2 "+err);
}
logger.infor("result "+result);
});
});
if(!isUpdate){
var db = modules.collectionAccess,
objectToSave = { name: request.body.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
res.continue();
}
});
}
}