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;
}
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.
I want to connect/ retrieve database item with backend no matter it’s let id or let something
When I’m using wixData.get(“myDatabaseName”, “chId”) it’s not taking database item I just simply want to retrieve database item in backend
To understand what I want to achieve read commented statement.
export async function getData() {
const apiKey = "xxxxxxxxxxxxxxx";
let cId = wixData.get("MyDataBaseName", "chId") // Here i want to retrieve database item field "chId" in backend to show the result in page repeater
.then((results) => {
let item = results;
return item;
})
.catch((err) => {
let errorMsg = err;
});
const response = await fetch("https://www.exampleapis.com/cha?&id=" + cId + "&key=" + apiKey, {
method: 'get'
});
For example my database item field “chId” = asdasd2313 then i want to take this value & put in the place of cId so it will look like this
cId = asdasd2313 & url is like
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;
}
Hi, thanks for your reply actually it’s not like that i just want to retrieve the database item in backend
For example: myDatabase (chId) holds the different items like - sda5414,ad4535ad,ads541 simply i want these values in “cId” . My code is running when i declare const cId = “some value”
All this code is for backend, i’m not able to understand how can i retrieve the database item in backend file
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.
Thanks for the reply & code it runs successfully on console but when i run getData() inside onReady function in front end using repeater it’s not showing any result here is my code:
@jayitsvcool You need to assign the results from the getData() method to a repeater inside the onReady function by adding the code below:
getData().then(response =>{
let data =response;
data.forEach((item) => {//Check for the results from backend
item._id = String(index++);
})
$w(‘#myRepeater’).data = data;
})
Then add an onReady() event to your repeater to display the content.
export function myRepeater_itemReady($item, itemData, index) {
$item(“#chaName”).text = itemData.cId;
}
Not working, youtube channel id’s not taking in URL (response to get data) to fetch the result can you review the full code here so you will better understand
in the place of cId it should take channel id from database field - chId
I have checked your site and could see the following:
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.
In image item is assigned to a URL field:
$item(“#channelLogo”).src = itemData.medium.url;// Should be assigned to an image field.
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.
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.