Add data to existing database

Hello i need some help with my code. i try to add data to existing database, when owner is logged and owned ID is same as in line then add data to this line. but my code stil create new line with correct owner ID but not add to existing line unfortunetly create new line. please help me Thanks

wixData . get ( “Miesta” , “_owner” )
. then (( item ) => {
item = firstPlan
wixData . save ( “Miesta” , item );
console . log ( item );
})
. catch (( err ) => {
console . log ( err );

To save data with a specific ID!!!
Instead of ID you also could try to use → “_owner”

import wixData from 'wix-data';
2
3// ...
4
5let toSave = {
6  "_id":          "00001",
7  "title":        "Mr.",
8  "first_name":   "John",
9  "last_name":    "Doe"
10};
11
12wixData.save("myCollection", toSave)
13  .then((results) => {
14    console.log(results); //see item below
15 })
16  .catch((err) => {
17    console.log(err);
18 });

hello, thanks but this is still same problem only with diferent ID or owner, i need get data from database find existing user in database and there add data to user not made new dataline. Every time this make new line instead of add to existing data.

if ( isLoggedIn ) {
wixData . query ( “Members/PrivateMembersData” )
. eq ( “_id” , wixUsers . currentUser . id )
. find ()
. then (( results1 ) => {
if ( results1 . items . length > 0 ) {
user . getPricingPlans ()
. then ( ( pricingPlans ) => {
let firstPlan = pricingPlans [ 0 ];
let plan = {
“_owner” : userId ,
“startDate” : firstPlan . startDate ,
“expiryDate” : firstPlan . expiryDate ,
“planName” : firstPlan . name
};

            console . log ( plan ); 

    wixData . query ( "Miesta" ) 
    . eq ( "_owner" ,  userId ) 
    . find () 
    . then (( results ) => { 
        if  ( results . items . length  >  0 ) { 
            wixData.save("Miesta", plan)  // I think problem is here this add new data to database but not for user in database.  
                .then((item) => { 
                console.log(item); //see item below 
                }) 
            . catch ( ( err ) => { 
            let  errorMsg  =  err ; 
            console . log ( errorMsg ) 
            }); 
          }  
      });        


i want to find line name Miesto and add there Startdate and Expirydate because that is same owner.

if ( isLoggedIn ) {
wixData . query ( “Members/PrivateMembersData” )
. eq ( “_id” , wixUsers . currentUser . id )
. find ()
. then (( results1 ) => {
if ( results1 . items . length > 0 ) {
user . getPricingPlans ()
. then ( ( pricingPlans ) => {
let firstPlan = pricingPlans [ 0 ];

    wixData . query ( "Miesta" ) 
        . eq ( "_owner" ,  userId ) 
        . find () 
        . then (( results ) => { 
            console . log ( results ) 
        **if** ( results . items . length  >  0 ) { 
            **let**  data  =  results . items [ 0 ]; 
            data . planNames  =  firstPlan . name 
            data . startDate  =  firstPlan . startDate 
            data . expiryDate  =  firstPlan . expiryDate 
            wixData . save ( "Miesta" ,  data ); 
            console . log ( data );  //see item below 
      }  **else**  { 
    // handle case where no matching items found 
  } 
}) 
        . **catch** (( err ) => { 
        console . log ( err ); 
}); 

i got it :smiley:

Try to complete the following CODE…
Still some codeparts missing, but you should be able to complete it now.


import wixData from 'wix-data';

$w.onReady(async function() {
    let isLoggedIn = true;
    let currentUserID = "here USER ID";

    if (isLoggedIn)     {console.log("You are logged-in, welcome! Let's find you inside our DB!");
        
        //find user-data from PMD (PrivateMembersData)
        let userData = await find_userData(currentUserID); 

        //show user-data if found...
        if (userData.length>0)  {console.log("FOUND USER-DATA", userData);  
            //get pricing-plans of current user...
            let pricingPlans = await get_PricingPlans(); console.log("Pricing-Plan: ", pricingPlans);
            
            
            
            
            if(pricingPlans) {console.log("Pricing-Plans for current user found!");
            
            
            
                wixData.query("Miesta")
                .eq("_owner", currentUserID)
                .find()
                .then((res) => {
                    if (res.items.length > 0) {
                        res.items[0].startDate = pricingPlans[0].startDate;
                        res.items[0].expiryDate = pricingPlans[0].expiryDate;
                        wixData.save("Miesta",res.items[0]) // I think problem is here this add new data to database but not for user in database. 
                        .then((item) => {console.log(item);})
                        .catch((err) => {let errorMsg = err; console.log(errorMsg);});
                    }
                    else {} 
                }) 
                .catch((err) => {let errorMsg = err; console.log(errorMsg);});
            }           
            else {console.log("No pricing-plans for current user found!");}
        }
        else {console.log("No user-data found!");}
    }
    else{console.log("?????????????");}
});            



//find user-data...
function find_userData(currentUserID) {
    return wixData.query("Members/PrivateMembersData")
    .eq("_id", currentUserID)
    .find()
    .then((res)=> {return(res)});
}


//Get pricing plan for user...
function get_PricingPlans() {
    user.getPricingPlans()
    .then((pricingPlans) => {
        let firstPlan = pricingPlans[0];
        let plan = {
            "_owner" : currentUserID,
            "startDate" : firstPlan.startDate,  
            "expiryDate" : firstPlan.expiryDate,
            "planName" : firstPlan.name
        };
        return (plan);
    }); 
}