Hi,
I'm working with 3rd party API & I'm using backend file (.jsw) to get the endpoint URL.
I got stuck on this backend code & I'm looking for the answer but I'm not able to get the answer yet
Basically I want to achieve a result on page repeater from backend logic.
I query the database successfully but I'm not able to retrieve the database value & insert that value in url so i can get the result in the page repeater
Here is my code of backend:
import { fetch } from 'wix-fetch';
import wixData from 'wix-data';
export async function getData() {
const apiKey = "xxxxxxxxxxxxxxx";
let id = wixData.query("myDatabaseName") // Here i want to take values from my database
.find()
.then((results) => {
console.log(results.items);
});
const response = await fetch("https://www.exampleapis.com/cha?&id=" + id + "&key=" + apiKey, {
method: 'get'
});
if (response.status >= 200 && response.status < 300) {
const res = await response.json();
return res;
}
let res = await response.json();
return res;
}
Frontend code to call in repeater:
import { getData } from 'backend/getdat.jsw';
$w.onReady(function () {
getData()
.then((response) => {
let chlName = response.items[0].snippet.title;
$w("#chaName").text = chName;
});
});
I hope I'll get the answer this time
I have checked your site and could see the following:
1) You have assigned repeater items to fields that are not in your database collection: $item("#channelName").text = itemData.title;// There's no field with title field key in the YouTubeChannels collection.
2) In image item is assigned to a URL field: $item("#channelLogo").src = itemData.medium.url;// Should be assigned to an image field.
3) If since you are getting data from an external source, you can't access it with the itemData parameter. It has to be from the response statement.
4) The dynamic page only displays one element because it is the only element that you have called in your code, response.items[0]. If you want to display more elements you can implement code to access more elements as well as navigation.
If you wish to get assistance you can hire a Wix partner to assist you with your code.
You don't need to specify item position inside the repeater onReady method.
Please change this code:
let channelName = data.items[0].snippet.title;
$item("#channelName").text = itemData.channelName;
To: $item("#channelName").text = itemData.title;
You have to do the same for the rest of the repeater items
You can refer to our API here: https://www.wix.com/corvid/reference/$w.Repeater.html#data
Please note that it is not possible to assign elements to a repeater in backend. As such, I suggest that you try accessing the database content from backend by adding an index variable to help you iterate through the result array as shown below: let index = 0;
let id = wixData.query("myDatabaseName", "cId") // Assuming cId is the field name
.find()
.then((results) => {
while(index <= results.items.length - 1) {
console.log(results.items[index].cId);//Assuming cId us the field key of the cId field
index++;
}); To see the results, please run getData() inside the onReady funtion in front end code. Should you need further assistance, please include your site editor URL so that we can take a closer look on our end.
If I understand you correctly, then your only issue is displaying the getData() method results in a repeater. If this is the case, I would suggest that you add the following code to your onReady function: getData().then(response =>{ let data =response; data.forEach((item) => {//Check for the results from backend console.log(item); }) $w('#myRepeater').data = data; }) Then add an onReady event to your repeater element and assign the title field value to the ''chaName text field as shown in the code below: export function myRepeater_itemReady($item, itemData, index) { $item("#chaName").text = itemData.title; }
If you are only wanting to get the members userID, then do something simple like this using eq function from Wix Data Query.
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#eq
So you could do something like one of these examples...
let id = query.eq("_id", userId);
wixData.query("myDatabaseName") .eq("_id", userId) .find();
Also, make sure that you have read about using Repeaters.
https://www.wix.com/corvid/reference/$w.Repeater.html
https://www.wix.com/corvid/reference/$w.Repeater.html#data
Plus, with your import on your page, you don't need the file name at the end of your backend import.
Also, should this be getdata or is getdat correct?
import { getData } from 'backend/getdat.jsw'; // don't need the .jsw file name.