Start a new topic
Answered

#3354 Business logic: remove query

Original query: "onPostSave" BL remove query doesn't seem to work although no errors are flagged. BL at the bottom of this email.


Questions from Kinvey:

1. Could you provide us with some data so that we can identify the specific transactions that were not deleted? 

Sure - the id in the "activeTransactions" collection is: 589f367d7c76a9f516b63e05

I'll leave this there for now - but this transaction should have been removed by the BL. The weird thing is, the log is telling me the remove method was executed successfully, but the data is still there.


2. Does the record you are trying to delete contain the ID "589b7f702262fa7555d80d6f"

I believe that one of the records. But I've deleted it manually now.


3. Are you attempting to delete the record using the DELETE method?

I'm using the "remove" method remove(query, callback)


4. Do you experience the same problem when attempting to delete the record using the app and also using the Kinvey API console? 

Only experience the problem through the app & BL. The records can be manually deleted through the console.


Business Logic "onPostSave" (activeTransactions collection):

(problem code in bold)


function onPostSave(request, response, modules) {

  var collectionAccess = modules.collectionAccess

      , logger = modules.logger

      , body = request.body

      , buyerEmail = body.buyerEmail

      , buyerFirstName = body.buyerFirstName

      , fxISO = body.fxISO

      , fxCostAmount = body.fxCostAmount

      , sellerEmail = body.sellerEmail

      , sellerName = body.sellerName

      , lxISO = body.lxISO

      , lxPurchaseAmount = body.lxPurchaseAmount

      , id = body._id

      , progress = body.transactionProgress

      , query = {"_id" : id};

      //weirdly this query works-> , query = {"buyerEmail": buyerEmail};

  

  logger.info(buyerEmail + ": " + progress);

  //logger.info("id:" + id);

  

 if (progress == "Sale complete" || progress == "Buyer cancelled payment" || progress == "Buyer cancelled" || progress == "Seller cancelled" || progress == "Payment error") {

       collectionAccess.collection('previousTransactions')

                   .insert(body, function (err, docs) {

                      if (err) {

                        logger.error('Data transfer fail: '+ err);

                        response.body.debug = err;

                        response.complete(500);

                      } else {

                        // previousTransaction dataStore insert complete

                        logger.info(buyerEmail +": " + progress + " --previousTransaction DataStore Update");

                        collectionAccess.collection('activeTransactions')

                              .remove(query, function (err, docs) {

                                if (err) {

                                  logger.error('Data transfer fail: '+ err);

                                  response.body.debug = err;

                                  response.complete(500);

                                } else {

                                  // activeTransaction dataStore delete complete

                                  logger.info(buyerEmail +": " + progress + " --activeTransaction DataStore Delete");

                                  response.continue();

                                }

                              });

                      }

                   });

 }

 response.continue();

}


Best Answer

Ramesh,


You will have to use the collectionAccess.objectID() to convert the string to Object Type.


Please take a look at:

http://devcenter.kinvey.com/html5/reference/business-logic/reference.html#objectid-note


Sample code below:


 

var query = {"_id" : collectionAccess.objectID("571e3f26a80e57053cc5f51e")};


collectionAccess.collection('xyz')

.remove(query, function (err, docs) {

if (err) {

logger.error('Data transfer fail: '+ err);

response.body.debug = err;

response.complete(500);

} else {


response.continue();

}

});



Thanks,

Pranav

Kinvey


Hi Ramesh,


Can you edit query = {"_id" : id} in your code according to the warning given here: http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#objectid-note?


Let me know once you have tried this out.



Regards,

Wani

Kinvey Support


Hi Wani


Thank you for the link. I've changed the query from:


query = {"_id" : id} 


to 


query = {_id : id};


but it's still not removing the entry from the collection, neither is it throwing up an error in the logger.


Thoughts?


Thanks

Ramesh

Answer

Ramesh,


You will have to use the collectionAccess.objectID() to convert the string to Object Type.


Please take a look at:

http://devcenter.kinvey.com/html5/reference/business-logic/reference.html#objectid-note


Sample code below:


 

var query = {"_id" : collectionAccess.objectID("571e3f26a80e57053cc5f51e")};


collectionAccess.collection('xyz')

.remove(query, function (err, docs) {

if (err) {

logger.error('Data transfer fail: '+ err);

response.body.debug = err;

response.complete(500);

} else {


response.continue();

}

});



Thanks,

Pranav

Kinvey

Hi Pranav


That worked. Thank you very much.


Ramesh

Ramesh,

Glad it is working. Happy to help!

Thanks,
Pranav
Kinvey

 

Login or Signup to post a comment