[Vuejs]-How to fix problem with arguments in function?

1👍

addUserLogs can receive an object with optional keys and merge the last received object with the new one:

export default async function addUserLogs(newParams) {
  const lastParams = <get the last params from storage>;
  const obj = Object.assign(lastParams, newParams);
  ...
} 

// usage examples
addUserLogs({amountName: 'x', serviceID: 1, terminalID: 2, isSaved: true ,isSentToGateway: false, amount: 10});
addUserLogs({isSentToGateway: false, amount: 10});
addUserLogs({amountName: 'x', serviceID: 1, terminalID: 2});

If you want to be more declarative about addUserLogs signature you can do something link:

export default async function addUserLogs({amountName, serviceID, terminalID, isSaved ,isSentToGateway, amount}) {
  const lastParams = <get the last params from storage>;
  const newParams = {
      amountName: amountName || lastParams.amountName, 
      serviceID: serviceID || lastParams.serviceID, 
      terminalID: terminalID || lastParams.terminalID, 
      isSaved: isSaved === undefined ? lastParams.isSaved : isSaved,
      isSentToGateway: isSentToGateway === undefined ? lastParams.isSentToGateway : isSentToGateway, 
      amount: amount === undefined ? lastParams.amount : amount
  };
  ...
}

0👍

In order to get ride of this and make your function more generic, You should provide default arguments to the function so that when you don’t pass any the oject creation here

   serviceID: service,
   terminalID: terminal,
   amountName: account,
   amount: amount,
   isSaved: isSaved,
   isSentToGateway: isSentToGateway,
 };

will not break. To do that you use the below format

export default async function addUserLogs(account="default", service = "default", terminal = "default value",isSaved = "default value",isSentToGateway = "default value",amount = "default Value" ) 

provide default only for variables that are not going to be required in other places when called.

Note when you call this function and provide params they will replace the default but when you don’t the function will use the default as such your function doesn’t break with undefined variables

👤harisu

0👍

in that example you can set null for some arguments that is not required and you don’t want to use them when callig your function.

you can use some filters for your arguments like so:

export default async function addUserLogs(account, service, terminal,isSaved,isSentToGateway,amount ) {
  const obj = {};

  if(account) obj.amountName = account;
  if(service) obj.serviceID = service;
  if(terminal) obj.terminalID = terminal;
  if(isSaved !== null) obj.isSaved = isSaved;
  if(isSentToGateway !== null) obj.isSentToGateway = isSentToGateway;
  if(amount) obj.amount = amount;

  const db = await InitDb();
  const tx = db.transaction('userLogs', 'readwrite');
  const store = tx.objectStore('userLogs');
  const index = await store.put(obj);
  let params = {};
  if (window.localStorage.getItem('params') !== null) {
    params = JSON.parse(window.localStorage.getItem('params'));
  }
  window.localStorage.setItem(
    'params',
    JSON.stringify({ ...params, userLogIndex: index })
  );
} ```

Leave a comment