[SOLVED] Changing Boolean in Database

hey guys need some help with changing the Open boolean (true) to (false)saved in the database of a positionId when the withdrawn function reaches onSuccess.

  // Query For Positions
  const queryPositionsId = useMoralisQuery("PositionsId");
  const positionsId = JSON.parse(
   JSON.stringify(queryPositionsId.data, [
     "positionId",
     "walletAddress",
     "createdDate",
     "unlockDate",
     "weiStaked",
     "percentInterest",
     "weiInterest",
     "open",
     "numDays"

   ])
  );
 

let results2 = positionsId.filter((e) =>( e.walletAddress.toLowerCase() === account && e.open === true) )
 console.log(results2)

  const withdraw = async (positionId) => {
    const readOptions = {
      contractAddress: CONTRACT_ADDRESS,
      functionName: "closePosition",
      abi: ABIStake,
      params: {
        positionId: positionId,
      } 
    };
    await contractProcessor.fetch({
      params: readOptions,
      onSuccess: () => {
        alert("WithDrawal sucessfull");
        console.log(positionId)
// NEED TO CHANGE BOOLEAN  OF OPEN TO FALES IN DATABASE
      },
    }) 
  }

 <div className="col-md-2 assets-text">
      {a.open ? (
                <div onClick={ () =>  {withdraw(a.positionId-1)}} className="orangeMiniButton"> 
                  Withdraw</div>
              ) : (
                <span>closed</span>
              )} 
</div>

Trying This now but its give an error obj not found. when i console logged console.log(query) i get this reply

  // Query For Positions
  const queryPositionsId = useMoralisQuery("PositionsId");
  const positionsId = JSON.parse(
   JSON.stringify(queryPositionsId.data, [
     "positionId",
     "walletAddress",
     "createdDate",
     "unlockDate",
     "weiStaked",
     "percentInterest",
     "weiInterest",
     "open",
     "numDays"

   ])
  );
 

async function updatePosition (positionId){
  console.log(positionId)
    const id = getPos(positionId).objectId
    const positionsId = Moralis.Object.extend("PositionsId");
    const query = new Moralis.Query(positionsId);
    console.log(query)

    await query.get(id)
    .then(obj => {
      obj.set("open" , false);
      obj.save()
    })
   }  
const getPos = () => {
  if (positionsId){

  const results2 = positionsId.filter(
    (e) =>
    e.walletAddress.toLowerCase() === account &&
    e.open === true
  )
 return results2
  }
}

  const withdraw = async (positionId) => {
    const readOptions = {
      contractAddress: CONTRACT_ADDRESS,
      functionName: "closePosition",
      abi: ABIStake,
      params: {
        positionId: positionId,
      } 
    };
    await contractProcessor.fetch({
      params: readOptions,
      onSuccess: () => {
        alert("WithDrawal sucessfull");
        updatePosition(positionId)
      },
    }) 
  }

what is the code that should change that boolean value?

async function updatePosition (positionId){
  console.log(positionId)
    const id = getPos(positionId).objectId
    const positionsId = Moralis.Object.extend("PositionsId");
    const query = new Moralis.Query(positionsId);
    console.log(query)

    await query.get(id)
    .then(obj => {
      obj.set("open" , false);
      obj.save()
    })
   }  
}
 const withdraw = async (positionId) => {
    const readOptions = {
      contractAddress: CONTRACT_ADDRESS,
      functionName: "closePosition",
      abi: ABIStake,
      params: {
        positionId: positionId,
      } 
    };
    await contractProcessor.fetch({
      params: readOptions,
      onSuccess: () => {
        alert("WithDrawal sucessfull");
        updatePosition(positionId)
      },
    }) 
  }```

while testing code i did mange to turn the 1st object to false so i guess its the object ID which is creating a problem in my code

async function updatePosition (positionId ){
    const query = new Moralis.Query("PositionsId");
    console.log(query) 

    await query.first()
    .then((obj) => {
      obj.set("open" , false);
      obj.save()
    });
   }  ```

Did you add objects by connecting directly to mongo db?

nope i used sync events from the smart contract

You could log that id, and also check if it is of the same type as in the database

is it possible to find the object using the positionId saved in the backend rather then the objectId

Yes using query constraints - you make a query to a class and filter the results e.g. if the object has a specific positionId (equalTo).

1 Like

Got it to work passed the objectId on the onClick Withdraw Function :slight_smile: thanks for the help @cryptokid and @glad

  async function updatePosition (objectId ){
    if (isAuthenticated ){
      const query = new Moralis.Query("PositionsId");
      await query.get(objectId)
      .then((obj) => {
        obj.set("open" , false);
        obj.save()
      })} }


 <div className="col-md-2 assets-text">
              {a.open ? (
                <div onClick={ async () =>  {await withdraw(a.positionId-1);
                updatePosition(a.objectId)}} 
                className="orangeMiniButton"> Withdraw</div>
              ) : (
                <span >closed</span>
               
              )} 
              </div>```
1 Like