How to get database value in repeater from backend?

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
#repeater #database #backend

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.
Repeater - Velo API Reference - Wix.com
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.

@givemeawhisky I think you misunderstood the ques.

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

url = https://www.exampleapis.com/cha?&id=asdasd2313&key=xxxxxxx

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:

Frontend (with repeater)

import { getData } from 'backend/getDat.jsw';

$w.onReady(function () {

    getData()
        .then((response) => {
 let chaName = response.items[0].snippet.title;
            $w("#chName").text = chaName;
        });

});

@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;
}

@samuele Here is my code but it’s not showing result without any error on a repeater or on a dynamic page.

//Frontend (With repeater)
import { getData } from 'backend/getDat.jsw';
$w.onReady(function () {
    getData().then(response => {

 let data = response;
 let index = 0;

        data.forEach((item) => { 

            item._id = String(index++);

        })

        $w('#myRepeater').data = data;

    })
});
export function channelRepeater_itemReady($item, itemData, index) {
    getData()
        .then((response) => {
 let chaName = response.items[0].snippet.title;
            $item("#chName").text = $item.chId;

//Frontend (Dynamic Page)
 
import { getData } from 'backend/getDat.jsw';
$w.onReady(function () {
    getData()
        .then((response) => {
 let chaName = response.items[0].snippet.title;
            $w("#chName").text = chaName;

@samuele I follow your suggestions but i’m not able to get result full code here: https://www.wix.com/corvid/forum/community-discussion/what-s-wrong-in-this-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

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

const response = await fetch("https://www.googleapis.com/youtube/v3/channels?part=statistics,snippet,brandingSettings&id=" + cId + "&key=" + apiKey, {
        method: 'get'
    });

When it starts taking channel id in the URL result will may show to the repeaters item

@Sam Eru Hey my website works but right now i’m facing two little issues can you tell what wrong Read here

@jayitsvcool We need your site editor URL and the page with this code in order to check the issue.

@samuele Here is my site link: https://editor.wix.com/html/editor/web/renderer/edit/6e724a7c-284f-4f1a-ace8-5aca4f4cf127?metaSiteId=4e871e0e-292e-47ec-9895-73cea2da3cb7&editorSessionId=53b6a18d-599a-4d58-97ec-0d3f6b1de8ce&referralInfo=dashboard

Issue 1: Results not showing on repeaters
Issue 2: Only one result showing on dynamic page

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.