How to retrive database item in backend file of wix?

Hi,

It’s been a month I’m looking for an answer on Wix but not find yet.
How can I retrieve the database item in the backend file of Wix (.jsw) I just want to take the items from the database in the backend.

If anyone knows or is it possible to do it.
Because I’m able to query my database item in backend but I’m not able to retrieve the items so I can use in my frontend or anywhere I want like with API’s

#database #corvid #backend

So in the JSW file write something like:

import wixdata from 'wix-data';
export function getItem(id){
wixData.get("CollectionName", id)
.then(r => return r);
}

and in the front end:

import {getItem} from 'backend/myFileName.jsw';
let toGet = "dasfads-dasfdsf-adsfsdaf";//use the item Id
getItem(toGet)
.then(r => {
//do whatever you want
});

Hi, thanks for your reply but if i want to retrieve the item value in this code how to do it?

Backend:

export async function getData() {

const apiKey = "xxxxxxx";
let cId = await function getItem(id) {
        wixData.get("useData", id)
            .then(r => return r);
    }
 const response = await fetch("https://www.exampleapis.com/nels?&id=" + cId + "&key=" + apiKey, {
        method: 'get'
    });

Frontend:

$w.onReady(function () {

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

});


@jayitsvcool I’m not sure what you’re trying to do, because when you get an item it is a full object while your cId is just a single string. You should explain what you’re trying to do.
(+ you have some errors in your code, but first explain your goal).

  • you have a function with a “id” variable, but I can’t see where you’re passing this variable to the function.

@jonatandor35 Actually it’s youtube live subs count so everything is working when declare const with channel id but when i declare let it’s not working i don’t know why
=> id is in endpoint URL

@jayitsvcool use more words.
Do you pass the Id from the front end? What item filed are you trying to extract and (and to assign to cId) etc… details are needed

@jonatandor35 Here is my full info

I’m trying to create a database where i can store all the channel id which is used to call live count of channels in api url & in page repeater item (different channels)
Database Name: youGrow
Database field key (where channel id are save) : chId ( Name: Channel Id)

Here is my full actual code
Backend:

 
import { fetch } from 'wix-fetch';
import wixData from 'wix-data';

export async function getData() {

 const apiKey = "xxxxxxxxxxxxxx";
 //const id = "UCBVjMGOIkavEAhyqpxJ73Dw"; (it works when i declare this)
 let cId = wixData.get("youGrow", "chId")
        .then(
 return chId);

 const response = await fetch("https://www.googleapis.com/youtube/v3/channels?part=statistics,snippet,brandingSettings&id=" + cId + "&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 (with repeater to show different channels live count):

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

 
$w.onReady(function () {
    getData()
        .then((response) => {
 let subCount = response.items[0].statistics.subscriberCount;
            console.log(subCount);
            $w("#liveCount").text = subCount;
        });

});

@jayitsvcool this line is wrong
wixData.get(“youGrow”, “chId”)
because with get() you must use the entry I’d. If you want to query for the chId field, you’ll need to use query().
you also have other mistakes but first fix this one

@jonatandor35 When i query it shows database item in the console

let cId = wixData.query(“youGrow”)
.find()
.then((results) => {
console.log(results.items);
});

@jayitsvcool you don’t query for a specific item. I throughout you wanted something like:

//declare chIdToFind
 wixData.query("youGrow") 
.eq("chId", chIdToFind )
.find()
//etc...

@jonatandor35 But i don’t want to query item i want to take database item so in the place cId in url it will take items from the database for every repeater item on-page that’s the main problem i’m facing right now i’m not able & understand how can i take database item in the place of cId in url so it will show the result in repeater

@jayitsvcool you’re tring to get an item from the collection. Base on what? What is the identifier you use to find the item. I just don’t see it in your code.
you wrote;

wixData.get("youGrow", "chId")

But that means it looks up for an item that its system Id is equal to the word “chId”. Is that what you wanted?

@jonatandor35 chId is my field key where all value stored in this field

@jayitsvcool I know and that’s why your get() function is wrong.
Sorry but your explanation is unclear. You never said what identifing data you use in order to find the specific chId value.

@jayitsvcool Only Wix personnel can access your editor. not me.

@jonatandor35 ok… i’ll try my best to explain my project i hope you understand

Basically i’m creating a website with 3rd party api (YouTube) where i’ll store channel id to call the channel info on repeater item that means every repeater item hold a new channel data the problem is when i declare channel id as a const my website shows the channel info but i want to take database field where i store all the channel id to fetch the channel info on repeater item. Please visit my code above to under stand better https://www.wix.com/corvid/forum/main/comment/5db9819f9cac49001731b070