Using client.db.update(Key,"remove") to remove a value in the Attribute

Currently, my app is maintaining a database with given company details. The purpose of the following function is to remove specific data from the attributes. I have stored values inside an attribute called ‘ID’ as an array:

"ID":["A1","A2","A3"]

Function:

function removeCompany(e){
  e.target.parentNode.remove(); 
  var removeID = console.log(e.target.id);
  client.db.update("TicketID","remove", [{"ID":removeID}]).then(function(data) {
    console.log(data)
  }, function(error) {
    console.log(error)
  });

When I ran this, I got a 422 error with the message: " Keys or paths inside attributes object cannot be blank"

Is it possible to remove a specific value?

Hi @Chathuranga_Suriyaar

Assuming your DB key is as follows :
TicketID = {
“ID” : [‘A1’, ‘A2’, ‘A3’]
}

For example, if you want to remove ‘A1’ from the ‘ID’ object of the TicketID DB you can try the below code snippet

client.db.update("TicketID","remove", ["ID[0]"]).then(function(data) {
  // success operation
  console.log(data);
}, function(error) {
  // failure operation
  console.log(error)
});

lets consider another case.
if ‘ID’: {‘A1’ : ‘aaa’ ,‘A2’ : ‘bbb’,‘A3’ : ‘ccc’}
then the following snippet will remove A1 key and its value

client.db.update("TicketID","remove", ["ID.A1"]).then(function(data) {
  // success operation
  console.log(data);
}, function(error) {
  // failure operation
  console.log(error)
});

Hope this is clear. You can modify the code snippet based on your requirement.

Please reach out in case of further queries.

Thanks and Regards,
Mughela Chandresh

2 Likes

Hi @Mughela_Chandresh -

Thank you for getting back to me! I implemented your code and made some modifications to it.

     for(var i=0; i<data.ticketId.length; i++){
       if(data.ticketId[i] == TicketID)
       {
            console.log(data.ticketId[i]);
            client.db.update(removeID,"remove", ["ticketId"[i],"Value"[i]]).then(function(data) {
              e.target.parentNode.remove(); 
              updateCompany(removeID);
           }, function(error) {
               handleErr(error.message);
          });
       }
     }

I am trying to remove the ticketId[i] and the value[i] with the given condition, but this is not working. I would really appreciate it if you could look at this as well. Thank you again!

Solution :

for(var i=0; i<data.ticketId.length; i++){
       if(data.ticketId[i] == TicketID)
       {
            console.log(data.ticketId[i]);
            client.db.update(removeID,"remove", [`ticketId[${i}]`,`Value[${i}]`]).then(function(data) {
              e.target.parentNode.remove();
              client.interface.trigger("showNotify", {type: "success", title: "success",message: "Company is removed"}).then(function(data) {}).catch(function(error) {});
              updateCompany(removeID);
           }, function(error) {
               handleErr(error.message);
          });
       }
     }

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.