Increase value in database depending on a field

Hi all,
i have a database called Groups, where the name of the social groups is stored, and each record has a ‘number of posts’ field:

When i add a post in a group, let’s say Passione Musica! (record 4), i want the relevant numberOfPosts value increased. I am using ’ eq ’ to filter, but i don’t know how to set the ‘then’ part:

wixData.query("Groups")
  .eq("groupName", $w('#text50').text) // text50 is the text where the Group Name is written in the dynaic page
  .find()
  .then( (results) => {
  ..... //here goes the rest of the code

I guess i must use a R/W dataset to read the curent value and increase it.
Anyone can help?
Thank you as usual.

You got it write →

var id
var totalCount 
 
 //under OnReady
 wixData.query("Groups")
  .eq("groupName", $w('#text50').text) 
  .find()
  .then( (results) => {
 totalCount = results.totalCount;
         id1 = Number(totalCount) + 1
         
         
         
         //submit button OnCick event
         $w('#submit').onClick((event) => {
         let toInsert = {
 "numberOfPosts": id1,
                    };
         });
 

Thank you Ajit, but i have a problem in inserting the code…
i have to modify it a bit because i already have a function thathandles the submission, so i cannot use the part:

$w('#submit').onClick((event) => {
         let toInsert = {
 "numberOfPosts": id1,
                    };
         });

Here is the full code of my page:

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';

export function button2_click(event) { // updates thumbnail preview
 if($w("#uploadButton1").value.length > 0) {
        $w("#uploadButton1").startUpload()
      .then( (uploadedFile) => {
            $w("#thumbImage").src = uploadedFile.url;
          })
      .catch( (uploadError) => {
            console.log("File upload error: " + uploadError.errorCode);
        console.log(uploadError.errorDescription);
      });
  } 
 else {
      }
}
export async function button1_click(event) { // submit button
 let toInsert = {
gPostCommenterId: $w('#dataset3').getCurrentItem()._id, //user profile dataset to gather data of the submitter
gPostCommenterImage: $w('#dataset3').getCurrentItem().profileImage,
gPostCommenterName: $w('#dataset3').getCurrentItem().fullName,  
gPostCommenterLocation: $w('#dataset3').getCurrentItem().location, 
gPostPost: $w('#textBox1').value,
groupName: $w('#title').text,
gPostPostTitle: $w('#input1').value,
groupId: $w('#text58').text,
gPostPostImage: await uploadFile(),
_createdDate: $w('#dataset5').getCurrentItem()._createdDate,  // droup post writeonly dataset connected to GroupPost database  to write the post
gPostNumberOfLikes : 0,
gPostNumberOfComments : 0
 }
function uploadFile(){
return $w('#uploadButton1').startUpload().then(r => r.url);
}

wixData.insert("GroupPosts", toInsert) //database that saves the posts
.then( (results) => {
let item = results; 
console.log("OK")

$w("#dataset4").refresh();
$w("#textBox1").value = " ";
$w("#input1").value= " ";
$w("#thumbImage").src= "";
$w('#button2').hide();
});
}

export function uploadButton1_change(event) { //enables thumbnail preview button for the image
 if($w('#uploadButton1').value.length > 0) {
   $w('#button2').show();
   }
 else {
      $w('#button2').hide();
     }
}

As you can see, the function for button1 handles the submission, and does many things:

  • gathers the data of the submitter (creator of the post)
  • gathers the data of the post (post, title, picture, id)
  • writes them to the GroupPosts database, setting as a new post, the likes and comments to 0
  • empties the fields used for the post

The fact is that i don’t know where to insert you code correctly. I have tried to insert it in a modified version before the export function uploadButton1_change(event) section but the resut is that the number of posts in the Database is not incremented.
I think there may be a problem because i am trying to do 2 wixData.insert to 2 different databases (GroupPosts for the coments, and Groups for the nuber of posts).

Hi Alessandro Demontis,
Is this what you are trying to do???

When the button is clicked, the comment is.inserted to groupComents database.
Then the no. of post is inserted to the Group database…

For the first function you have already…
The second function, as I see, there is no need for insert but to update… https://www.wix.com/corvid/reference/wix-data/update

You have to update the no.of post field…

Best reference https://www.wix.com/corvid/forum/community-discussion/typeerror-error-capturestacktrace-is-not-a-function?origin=member_posts_page

Something like this →

wixData.query( “Groups” )
.eq( “groupName” , $w( ‘#text50’ ).text)
.find()
.then( (results) => {
let id = results.items[0]. noOfPosts
id1 = Number(id) + 1

     //submit button OnCick event 
     $w( '#submit' ).onClick((event) => { 
     **let**  toUpdate = { 

" numberOfPosts " : id1,
};
wixData . update (“Groups”, toUpdate )
});