Calling Collection data to Frontend email subject <SOLVED>

Honestly, i’m super confused about how to call backend data and make it work on the front end when it isn’t an element that is directly connected to collection data. In my use case, I have a button that says “contact us” that i want to have open an email up, directed to the same email (no matter the dynamic page) but i want the subject to change depending on which page its on. I want the subject to say " Looking to book {‘fullName’}, where ‘fullName’ is a text field in my collection. I dont really understand how to go from data on the back end, to usable text on the front end!

import wixData from 'wix-data';

$w.onReady(function () {
wixData.get("Portfolio")
$w('#textSeriesName').text = "title";
    $w("#button1").link = `mailto:agent@sophia.com?subject=Looking to book ${"#textSeriesName"}`;
});

@bwprado I just saw your reply here: https://www.wix.com/velo/forum/coding-with-velo/search-collection-and-display-result-in-text-object

this is a different scenario, but your explanation was so good! Do you think you could help me out haha.

Seems like you mix something.
You are sure that you understand what is …
a) BACKEND
b) FRONTEND
…???

Why do you need BACKEND in your case?

I think your problem will be, that you can’t get data out of your COLLECTION/DATABASE , right ???

Ha, okay maybe I’m using the terminology wrong. That aside, I need a piece of wix collection data (a text field) to Appear in my subject text for the email I’m coding in my dev console, and it needs to change depending on the dynamic page I’m on

Exactly what i already assumed… :stuck_out_tongue_winking_eye:

Since you are using a → DYNAMIC-PAGE ← you will need the following…

let dbField = "xxxxxxxxxxxxxxxxxxxxx" //<<--- which DATABASE-FIELD ?

$w.onReady(function() {
	$w('#dynamicDataset').onReady(()=>{
		let currentItemData = $w('#dynamicDataset').getCurrentItem();
		console.log("Current-Dynamic-Data: " currentItemData);
		$w('#textSeriesName').text = currentItemData[dbField];
		
		
		$w("#button1").link =`mailto:agent@sophia.com?subject = Looking to book ${"#textSeriesName"}`;
	});
});

Something like the shown example.
Surely you will have to modify it a little bit for your own needs.
Also check the CONSOLE-LOGS inside of → CONSOLE.

Hey @russian-dima Thank you for your help so far!

Okay, so…

I replaced this code:

import wixLocation from 'wix-location';
let path = wixLocation.path

$w.onReady(function () {
    $w("#button1").link = `mailto:agent@sophiamodels.com?subject=Looking to book ${path}`;
});

Which opened an email to the right person, with almost the right subject, but the path ended up being longer than i wanted, so i tried what you said
with this:

let dbField = "title" //<<--- which DATABASE-FIELD //where "title" is the field key within the collection, and the collection ID is "Portfolio"

$w.onReady(function() {

$w('#dynamicDataset').onReady(()=>{
//where dynamicDataset is the name of my the dataset on my page
        let currentItemData = $w('#dynamicDataset').getCurrentItem();
        console.log("Current-Dynamic-Data: " 
// should the " be on the other side of the :?
        currentItemData);
        $w('#textSeriesName').text = currentItemData[dbField];
        
$w("#button1").link = `mailto:agent@sophia.com?subject=Looking to book ${"#textSeriesName"}`;
    });
});

When i clicked the button using this code, nothing opened up, but i feel like im getting closer haha!

I forgot a simple ----> , <----- (semicolon)

WRONG!
console . log ( "Current-Dynamic-Data: " currentItemData );
RIGHT
console . log ( "Current-Dynamic-Data: " , currentItemData );

let dbField = "title" //<<--- which DATABASE-FIELD ?

$w.onReady(function() {

    $w('#dynamicDataset1').onReady(()=>{
        let currentItemData = $w('#dynamicDataset1').getCurrentItem();
        console.log("Current-Dynamic-Data:", currentItemData); 
        $w('#textSeriesName').text = currentItemData [dbField];
        
        $w("#button1").link = `mailto:agent@sophiamodels.com?subject=Looking to book ${"#textSeriesName"}`;
    });
});


that fixed it mostly. Now when i click, i get this. the error i’m getting is:

this seems like it’s supposed to be something I’m naming, but i’m unsure of what element the ID its supposed to be referencing. From my (maybe misguided) understanding, the code is pulling the currentItemData, or the “title” data for the current dynamic page from my dataset associated with #dynamicDataset1, AND THEN, turning that title into a line of text that is being inputted in the subject line, which has been named #textSeriesName?

am i getting warmer haha?

Ok, one more time!
Let’s make a little simple example…

Since i do not want to prepare a whole dynamic data-stuff and add a dataset, i just create my own example-data-object including some data… —> currentItemData

This is how it looks like…

let dbField = "title" //<<--- which DATABASE-FIELD ?

let currentItemData = {};

$w.onReady(function() {
    currentItemData.title = "myTitleHere";
    currentItemData.firstname = "myFirstNameHere";
    currentItemData.lastname = "myLastNameHere";


    //$w('#dynamicDataset1').onReady(()=>{
    //    let currentItemData = $w('#dynamicDataset1').getCurrentItem();
        console.log("Current-itemData:", currentItemData); 
        $w('#textSeriesName').text = currentItemData [dbField];
        
        $w("#button1").link = `mailto:agent@sophiamodels.com?subject=Looking to book ${currentItemData}`;
    //});
});

Maybe you will still think, that it is not working right?
Because you get an → OBJECT ← inside of your EMAIL ?

This is right and at same time, wrong, but at least still RIGHT!:grin:

Already confused? Ok, let’s let’s solve the mystery…

What about your - - > dbField ← - you forgot already, right?

let dbField = "title"

Let’s expand our code a little bit…

$w("#button1").link = `mailto:agent@sophiamodels.com?subject=Looking to book ${currentItemData[dbField]}`;

What happens if you change from → “title”
…to —> “firstname”…
…or to ----> “lastname” ???

Amazing! Thank you so much for your help.
Here’s the final code:

let dbField = "title"
$w.onReady(function() {
    $w('#dynamicDataset1').onReady(()=>{
        let currentItemData = $w('#dynamicDataset1').getCurrentItem();
        console.log("Current-Dynamic-Data:", currentItemData); 
        $w("#button1").link = `mailto:agent@sophiamodels.com?subject=Looking to book ${currentItemData[dbField]}`;
    });
});

I appreciate you helping me learn this!