So I have looked at the wix documentation and have attempted to write two functions to run a query on my contacts in my database and delete a contact in my database. I’ve gotten the first to work but not sure I understand how to pass the data in the delete function or how to build the endpoint, any help is appreciated. Also the paths don’t understand how that works.
import { created , serverError , ok , badRequest , notFound } from ‘wix-http-functions’ ;
import wixData from ‘wix-data’ ;
export function get_myFunction ( request ) {
//URL is : https://brandywinetechsrvllc.wixsite.com/velo/_functions/myFunction/get
let options = {
“headers” : {
“Content-Type” : “application/json”
//“Access-Control-Allow-Origin”: “*”
},
}
let dataOptions = {
“suppressAuth” : true ,
“suppressHooks” : false
};
// query a collection to find matching items
return wixData . query ( “PhoneBook” )
// .eq(“firstName”, request.path[0]).eq(“lastName”, request.path[1])
. find ( dataOptions )
. then ( ( results ) => {
console . log ( request . path [ 0 ]); // your site/velo/_functions/request.path[0]/request.path[1]
console . log ( results );
// matching items were found
if ( results . items . length > 0 ) {
options . body = {
"items" : results . items
};
**return** ok ( options );
}
// no matching items found
options . body = {
//"error": `'${request.path[0]} ${request.path[1]}' was not found`
"error" : `'it went wrong`
};
**return** notFound ( options );
} )
// something went wrong
. catch ( ( error ) => {
options . body = {
"error" : error
};
**return** serverError ( options );
} );
}
export function delete_myFunction ( request ) {
const id = request . path [ 0 ];
//URL may be: brandywinetechsrvllc.wixsite.com/velo/_functions/myFunction/delete/{4f0df655-6644-4955-a249-9ae90e4dbbd4}
let options = {
“headers” : {
“Content-Type” : “application/json”
}
};
// delete the item from a collection
return wixData . remove ( “PhoneBook” , id )
. then ( ( results ) => {
options . body = {
"deleted" : results
};
**return** ok ( options );
} )
// something went wrong
. catch ( ( error ) => {
options . body = {
"error" : error
};
**return** serverError ( options );
} );
}