accessing wixData from backend module

Hi,
I had some code sitting on my main page in the code editor, and I decided it was best to move it to the backend via a .jsw file.

I’ve written the export and import functions correctly and saw that I can console.log something, but I have the line:

wixData.get(“ifc_data”).find().then((results) => { console.log stuff… }

which worked in the front-end, but now in the back-end it doesn’t seem to fetch the data via the .find() function.

Any idea why this is or perhaps I should use .get() or some other function from the API?

Hi Yafim,

wixData.get(“ifc_data”).find() is not valid usage of wixData. I think you meant to do :

wixData.query("ifc_data").find()  

also check that you have imported wixData in new backend file.

import wixData from "wix-data" 

Let me know if that helped.

Hi,
Sorry that didn’t help. I originally had the .query actually. Now my code on the backend is:

import wixData from ‘wix-data’;

export function ifc_data() {

  console.log('A'); 
  wixData.query("ifc_data").find().then((results) => { stuff } 

}

and on the front end it is:

import {ifc_data} from ‘backend/ifc_data.jsw’;
ifc_data();

So I can see it does print ‘A’, but it doesn’t perform any action inside of the query…

Would appreciate anyone who has insight into this issue

Hi,
What are you trying to achieve? do you want the data to be available in frontend through backend logic? or just console.log it?

Hi,

Was there any resolution to this issue? I have the exact same problem - my server module just does not seem to enter the wixdata.query.find code at all:

import wixData from ‘wix-data’;
export function init()
{
console.log(‘A’);
wixData.query(‘Courses’)
.find()
.then((results) => {
console.log(‘B’);
return results.items.length;
})
. catch ((err) => {
console.log(‘error’);
return err;
});
}
console will log ‘A’, but not ‘B’ or ‘C’. It doesn’t make sense. This code works fine when it is in the front end.

I used all my code in the front end eventually, so I didn’t really resolve this but I just didn’t use the .jsw file…

Thanks for the update Yafim. I actually managed to get the server code to access and return a collection in the end - I’m not 100% sure what made it work, but note the use of “async” and "await. Eventually it worked:

Server module:

import wixData from 'wix-data';

export async function getUniqueValues(returnColumn, filters) {
 let query = wixData.query('Courses');
 filters.forEach(function (filter) {
        query = query.eq(filter.field, filter.value)
 });
 let results = await query.find();
 return getUniqueColumnValues(results.items, returnColumn);
}

function getUniqueColumnValues(items, returnColumn) {
 var returnColumnValues = items.map(item => item[returnColumn]);
 return [...new Set(returnColumnValues)];
}

Front-end:

import {getUniqueValues} from 'backend/serverModule';


function populateDropdown(returnDropDown, returnColumn, filters) {
    getUniqueValues(returnColumn, filters)
        .then(function(distinctReturnColumnValues)
        {
 let options = buildOptions(distinctReturnColumnValues);
            options.push(_defaultOptions.find(function (item) {
 return item['id'] === returnDropDown.id
            }));
            options.sort(function (a, b) {
 return a.value.toUpperCase() === b.value.toUpperCase() ? 0 : a.value.toUpperCase() < b.value.toUpperCase() ? -1 : 1
            });
            returnDropDown.options = options;
            returnDropDown.enable();
        });
}

What i’m doing wrong can you suggest me the code:

Backend Code:

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

export async function getData() {

 const apiKey = "xxxxxxxxxxxxxxxxx";
 //const id = "xxxxxxxxxxxxxxxx"; (it works when i declare this but i want to take values from my database)
 let id = wixData.get('myDatabaseName'.'title');
 

 const response = await fetch("https://www.exampleapis.com/&id=" + id + "&key=" + apiKey, {
        method: 'get'
    });

Frontend code:

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


$w.onReady(function () {

    getData()
        .then((response) => {
 //let subCount = response.items[0].example;
 let fName = response.items[0].example.title;
            $w("#pName").text = fName;
        });

});


I’m trying to to achieve the data to be available in frontend through backend logic can you help me please?

What does you getData() method actually return? If I was fetching an array from an external api my method would look like this:

export function getdata() {
	return fetch(`${_apiEndpoint}`, { method: 'get' })
		.then(response => response.json())
		.then(json => json);
}

My page code would use it like this:

export async function doSomethingWithData() {
	await getdata().then(data => {
		//do something with data
	});
}

Actually getData() returns a url which show the statistics of website but here i want to take stored values from my wix database in let id that means my backend code connected to my database field which call in front end, My code is running successfully when i declare one value like: const id = ‘xxxxxx’ but i want to take the values from database, i hope you understand