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
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
});
@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).
@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
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):
@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 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?
@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.
@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