-
Hi, everyone. I’m new to Corvid forum, wasn’t quite familiar with the rules, please let me know if i do anything stupid. I’m trying to retrieve all the data in a userinput dataset and display them by group. The purpose of this is to get the those items details and send to clients by email.
-
For example :
-
Product: TV ; Rent Days: 7 ; Quantities: 10
-
Product: Projector ; Rent Days: 19 ; Quantities: 6
-
I’ve tested results.items[0], results.items[1], results.items[2]. It shows the all details about item 1,2&3 which is exactly what i want, but i the item count is limited if i code this way, when i tried with results.items[i], it just doesn’t work.
-
My code look like this
-
function GetProducts () {
-
wixData.query(“Userinput”)
-
.find()
-
.then( (results) => { if (results.items.length > 0)
-
{
-
let i = 0;
-
for (i = 0; i < 4; i++)
-
{
-
let Items = results.items[i];
-
ProductName = Items.email;
-
}
-
console.log(ProductName);
-
return ProductName; //see item below
-
}
-
else {}
-
return ProductName;// handle case where no matching items found
-
} )
-
. catch ( (error) => {
-
console.log(error.message);
-
console.log(error.code);
-
})
-
;
-
}
-
I’ve checked this post which is very helpful but i didn’t figure it out. I really appreciate any help, Thanks
Your post format could use some cleaning up but here we go.
wixData.query("Userinput")
.find()
.then( (results) => {
if(results.items.length > 0) {
let items = results.items; //(a)
} else {
console.log('No items found :(');
}
});
(a) is where you get all the items under the variable name ‘items’
Now, I’m not sure what’s the next step you want to take.
-
Do you wish to send all the items under the variable ‘items’ to a single email?
-
Send each item to an email provided in the item’s (item.email) field?
Thanks for pointing that out, I’ll definitely clean up my code for future posts. Here’s the process,
- users choose the items they want with quantities and other details, data stored in Userinput dataset
- users put in their personal details and the product details stored earlier will be sent to users’ email in the personal details they just type in.
So i want to send all the product with details they selected to the email they typed in a form.
@christopher42641 What you are trying to do is advanced.
Take a look at how to send emails via the SendGrid API using Corvid .
To place all the item details in your email’s body you can use the .forEach() function to create rows. Something like the below for your function which sends the email:
function sendEmail(items) {
let subject = 'Your Items';
let recipient = $w("#email").value;
let body = `Hi, <br>
<br>
Please find the item details below<br>
<br>
<b>Product Details Below</b><br>`;
items.forEach( (item, i) => {
let itemName = item.productName;
let quantity = item.quantity;
let price = item.price;
body = body + `<br><b>Product Name:</b> ${itemName} | <b>Qt:</b>
${quantity} | <b>Price:</b> ${price}<br>`;
});
sendClient(subject, body, recipient); //send email
}
@shantanukumar847 Thank you so much for you time and the quick response ! I really appreciate it. Some other forums i used to work with takes days to reply. I’ll look in to that later. Now i know i can get all items from results.items and all the results are grouped in
[ItemA, quantities(A), RentDate(A), Period(A), ItemB, quantities(B), RentDate(B), Period(B)] and they are result.items[0],result.items[1].
How can i display all the items based on users’ numbers of selection and why wouldn’t the for loop work for result.items[i]?
Or should i assign the collection’s value to an array and display ?
@christopher42641 The results.items is an array itself.
You can Query items based on user selection by using functions from the WixDataQuery
@shantanukumar847 Oh, I see. Thank you. But i don’t want to filter the dataset, i want to send those item users already selected. Say i have 100 items in my inventories, one user selected only 5 of them with specific details while another user selected 10. I want to code in the way they both gets all the details of their selected items.
Now the results.items[ ] shows specific one item with details, is there a way i can let the code runs though the dataset and gets all and display. I know results.item[ ] is the key so i used for loop.
@shantanukumar847 Thank you so much for the document. I managed to get the filtered result from my dataset with reference from this post . Here is my code
let allItems = ;
let PdName=;
let PdQuantities = ;
let PdDuration = ;
for ( var i=0;i<3;i++)
{
let results = await wixData.query(“Userinput”)
.limit(100)
.find();
let item = results.items;
let allItems[i] = item[i]; // assign specific item to allitems
let PdName[i] = allItems[i].title; // to get dataset’s field value
let PdQuantities[i] = allItems[i].quantities;
let PdDuration[i] = allItems[i].rentalDate;
}
As for the email sending function, here is the video that works for me. Here is the wix article for email sending
hope the above can help those who are also struggling.