Storing data in collection via backend jsw (wix-data insert).

Hello,

I have been trying to figure out how to store a form in a collection. The table has 2 reference fields.

I am getting the data from the form with the code below:

import { insertTaskData } from ‘backend/tasksForm.jsw’ ;

let newDetails = {
taskname : $w ( ‘#inpTaskName’ ). value ,
taskdescription : $w ( ‘#inpTaskDesc’ ). value ,
tasktype : $w ( ‘#selectTaskType’ ). value ,
taskStartDate : $w ( ‘#dtpTaskFromDate’ ). value ,
taskEndDate : $w ( ‘#dtpTaskToDate’ ). value ,
taskStatusId : $w ( ‘#selectTaskStatus’ ). value ,
taskSummary : $w ( ‘#rtbTaskSummary’ ). value ,
taskSubTasks : null ,
taskDocument : null ,
taskLocation : null ,
taskCountry : $w ( ‘#selectTaskCountry’ ). options [ $w ( ‘#selectTaskCountry’ ). selectedIndex ]. value ,
taskCity : $w ( ‘#selectTaskCity’ ). options [ $w ( ‘#selectTaskCity’ ). selectedIndex ]. value ,
interestEndDate : $w ( ‘#dtpInterestEndDate’ ). value ,
}

        // Check to see if the values are new or not, and add the ID if it's not 
        console . log($w ( '#selectTaskCountry' ). options [ $w ( '#selectTaskCountry' ). selectedIndex ]. value ); 
        console . log($w ( '#selectTaskCity' ). options [ $w ( '#selectTaskCity' ). selectedIndex ]. value ); 
        // Call the backend function 

        const  insertResult  =  **await**  insertTaskData ( newDetails ); 

The implementation at the backend file is as follows:

export async function insertTaskData ( task ) {
/* Check if the details are new or not by checking if they have a valid “_id”
If the details are new, just insert them in the database, if not we need to get
the old details and update them */

let  taskname  =  task.taskname ; 
let  taskdescription  =  task.taskdescription ; 
let  tasktype  =  task.tasktype ; 
let  taskStartDate  =  task.taskStartDate ; 
let  taskEndDate  =  task.taskEndDate ; 
let  taskStatusId  =  task.taskStatusId ; 
let  taskSummary  =  task.taskSummary ; 
let  taskSubTasks  =  task.taskSubTasks ; 
let  taskDocument  =  task.taskDocument ; 
let  taskLocation  =  task.taskLocation ; 
let  taskCountry  =  task.taskCountry ; 
let  taskCity  =  task.taskCity ; 
let  interestEndDate  =  task.interestEndDate ; 

  const  results  =  **await**  wixData . query ( "Countries" ). eq ( "code" ,  taskCountry ). find (); 
  let  itemcountryid  =  results.items [ 0 ]. _id ; 

  const  results1  =  **await**  wixData . query ( "CountryCities" ). eq ( "iso2" ,  taskCountry ). eq ( "city_ascii" ,  taskCity ). find (); 
  let  itemcityid  =  results1.items [ 0 ]. _id ; 

let  toInsert  = { 
    'dbf_tasktitle' :  taskname , 
    'dbf_taskDescription' :  taskdescription , 
    'db_taskTypeId' :  tasktype , 
    'db_taskStartDate' :  taskStartDate , 
    'db_taskEndDate' :  taskEndDate , 
    'db_taskStatusId' :  taskStatusId , 
    'db_taskSummary' :  taskSummary , 
    'taskSubtaskId' :  taskSubTasks , 
    'db_taskDocument' :  taskDocument , 
    'db_taskLocation' :  taskLocation , 
    'db_taskCountry' :  itemcountryid , 
    'db_taskCity' :  itemcityid , 
    'db_interestEndDate' :  interestEndDate 
} 

// Update the details in the database. 
// const res3 = await wixData.insert('Tasks', toInsert, options); 
let  options  = { 
    "suppressAuth" :  **true** , 
    "suppressHooks" :  **true** 
}; 

let  results2  =  **await**  wixData . insert ( "Tasks" ,  toInsert ,  options ) 
    . then (( results ) => { 
        let  item  =  results ;  //see item below 
        **return**  item ; 
    }) 
    . catch (( err ) => { 
        let  errorMsg  =  err ; 
        **return**  errorMsg ;  
    }); 

**return**  results2 ; 

}

The information is not stored in the Tasks table and I am getting the error below in the console:

You are dealing with Promises using both async/await and code and the .then() method. You should only use one or the other.

Try this at the end:

...
try {
  return await wixData.insert('Tasks', toInsert, options)
} catch (error) {
  console.error(error)
  throw error
}
...

Spot on. Thank you sir.