Start a new topic

how to check if a collection exists or not

I want to check wheather collection exists or not like below 


//---pseudo code start

var Kinvey = require('kinvey-nativescript-sdk').Kinvey;

var dataStore = Kinvey.DataStore.collection('collection-name');


if(datastore.collection.exists() == true){

//----do something

else{

----nothing

}

//---pseudo code end



1 Comment

Hello,


Kinvey doesn't provide such functionality out-of-the-box. You will have to create a custom endpoint as mentioned on this link and use "collectionExists(callback)" method of Collection Access module. Please use the following code for your custom endpoint:


function onRequest(request, response, modules) {
  
  var logger = modules.logger;
  var name = request.body.collectionName;
  logger.info("Collection name is " + name);
  
  modules.collectionAccess.collection(name).collectionExists(function (err, docs) {
    if (err) {
      logger.error("There was an error while processing this request " + JSON.stringify(err));
    } else {
    
      if(docs.exists==true){
        
        response.body = "Collection found";
        response.complete(200);
      }else{
        
        response.body = "Collection NOT found";
        response.complete(200);
      }
      
      response.complete(200);
    }
  });
  
  
  response.complete(200);
}




Once your custom endpoint is ready, you can test it from the API console as mentioned on this link. Please check the following screenshot:




Thanks,

Pranav

Kinvey





Login or Signup to post a comment