Query database, retrieve data, to implement in new query

Hello,

I’m using WIX code to query one database, and retrieve an user email, which is stored in a variable to be used for another query. If I type in the email address it works, but I am unable to retrieve the email address from the first query.

Database 1 called : “Members”
Database 2 called : “StudentRecordsDB”

Member logs in then is directed to this page, where both of these databases have been imported.

let selectedCategory = “”;
let emailholdervr = $w(“#emailholder”).value;

wixData.query(“Members”)
.eq(“title”, emailholdervr) <---- QUESTION IS HERE
.find()
.then( (results)=> {
let firstItem = results.items[0];//First item in results
selectedCategory = firstItem._id;

wixData.query(“StudentRecordDB”)
.eq(“usemail”,selectedCategory)
.find()
.then ((results)=> {
$w(“#repeater1”).data = results.items;
})
.catch( (err) => {
let errorMsg = err;
});
});

I am using a form field to retrieve the email address from the database “Members”. The email appears perfectly on the page, but just isn’t being passed through the code.

This works if I type in the email:
let emailholdervr = “tommy111@tommy.com


Thank you for any suggestions

3 Likes

Hi Collin,

Could you please describe what is #emailholder in your context?
Maybe share a link to your site editor?

Could it be that the retrieval of the email is don based on a future response that is not being wait for?
you could test that by trying to console.log the value you get, then preview to see what is actually being printed

Thanks
Adi

So, I’m trying to retrieve data from the database, then put this data ‘string’, into a variable that can be used in my code. But, I’m not too sure how to do this. So, I set up an input field that is linked to the database, then I’m trying to retrieve that data as a value in the code. #emailholder is the name of the input field, which works. The field shows the proper users email, but I just can’t get the field data to show up as a variable in my code.

Hi Collin,

Since there is a dynamic component (#emailholder) which is connected to a dataset (#membersDataset?), you should use its onReady action trigger to ensure the data is loaded before you try to consume it by code

For more details:

Example:

import wixData from 'wix-data';

$w.onReady(function () {
 $w("#membersDataset").onReady(function () {
   const userEmail = $w("#emailholder").text;
   console.log("userEmail = "+ userEmail);	
   wixData.query("Memebers")
 	  .eq("email", userEmail) 
          .find()
          .then( (results)=> {
             console.log("userTitle - " + JSON.stringify(results)); 
           }).catch( (err) => {
             console.log("Ooopsi... - " + errorMsg); 
           });
  });
});

Let me know if you need more details

Thanks
Adi

Thank you for your suggestions. I tried out your code and realized that it is half of what I need. Actually, I need to query the Members dataset to get the email, then use that email to query the dataset “StudentRecordDB” dataset.

Logically I think we are closer, but currently the repeater is dumping all the entries in the database. But, I only want the entries where the email from “Members” dataset = the email from “StudentRecordDB” dataset.

Here is the code I used, but still not getting the results I need.

import wixData from ‘wix-data’;

$w.onReady(function () {
$w(“#Members”).onReady(function () {

const userEmail = $w(“#emailholder”).value;
console.log(“userEmail = “+ userEmail);
wixData.query(“StudentRecordDB”)
.eq(“usemail”,userEmail)
.find()
.then ((results)=> {
$w(”#repeater1”).data = results.items;
});
}).catch( (err) => {
let errorMsg = err;
});
});

const userEmail = $w(“#emailholder”).value;
const userEmail = $w(“#emailholder”).text;

I tried both, and both don’t work
(Thank you)

This is the error that I receive:

TypeError: $w(…).onReady(…).catch is not a function

The catch should be on the “find”, not on the onReady, see the example Adi wrote

Hi Good day Everyone!

May I insert my self here?
How can I filter my database collection by owner ID?
I have a code but it’s not working. see below

$w.onReady(() => {

	wixData.query("regdog")
		.eq("_owner", tittle)
		.find()
		.then((results) => {

			$w('#repeater1').data = results.items;

		})

	.catch((err) => {
		console.log(err);
	});
});

and in the line with .eq, where can I link tittle?

.eq(“_owner”, tittle)
Thank you,
Geo

Hello All,
First I will post the code that is working perfectly, after receiving help from Adi and everyone above, thank you so much!! I have set up a relational database, which connects the fields of two databases. If you view the images I posted of my databases above, I have a field called ‘usemail’ in the database called ‘StudentRecordDB’. And, I there is a field called ‘Title’, in my ‘Members’ database. These two fields are linked together. So, the code I used is specific to retrieving information from one databse that is linked to another database. (Here is the code)

import wixData from ‘wix-data’;

let selectedCategory = “”;
let userEmail = “”;

$w.onReady(()=> {
$w(“#emailholder”).hide();//this is an input field that gets the user email, I don’t want it visible
$w(“#Members”).onReady(() => {

	const userEmail = $w("#emailholder").value; 
	wixData.query("Members") 
	.eq("title", userEmail) 
	.find() 
	.then( (results)=> { 
		let firstItem = results.items[0];//First item in results 
		selectedCategory = firstItem._id; 

		wixData.query("StudentRecordDB") 
		.eq("usemail",selectedCategory) 
		.find() 
		.then ((results)=> { 
			$w("#repeater1").data = results.items; 
			}) 
			.catch( (err) => { 
			let errorMsg = err; 
			}); 
	}); 
}); 

});

In response to Geo’s inquiry,
Where are you populating the ‘tittle’ variable? Is there a field on your interface where the user can type in the owner id? Or, a pull down menu? Or, are you retrieving information from one database to query and filter another database? (That is what I did)

You can do a couple quick test. Let’s say one owner id in your database = “Marley555”

Type this code for test purposes.

let tittle = “Marley555”; //example owner id in your database

If this works, then you can set up a field on your interface, where the user can type in an owner id, and a button to call a function to run the query script.

Hi Collin,

Good day, thank you for the response. :slight_smile:
Yes EXACTLY! I’m trying retrieve information from one database to query and filter and display the filtered item into a repeater. (Like what you did).

If that member of my website enter multiple item, all the item member enter into my website will query and filtered and it will display into a repeater.

Thank you,
Geo

Let me know if the advice I gave you above worked.

Can you tell me how can i pass the data from my backend to front end that means i have a database where i store values so i can call those value from my backend & show the result to the front end on the repeater by fetching the api.
My project is created with 3rd party api but the problem is when i declare const it will show result in front end but when i declare let val = wixData.get(“myDatabaseName”, ‘title’) it is not taking my database field value

My backend Code

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

export async function getData() {

 const apiKey = "xxxxxxxxxxxxxxxxx";
 let val = wixData.get("myDatabaseName", 'title');
 const response = await fetch("url goes here &id=" + val + "&key=" + apiKey, {
        method: 'get'
    });

Hi Collin, I have three questions about Corvid, I wait for your support, please.

  1. Do you have in Corvid any library to make a semantic query via SPARQL or another mechanism to a knowledge base compatible with RDF?
  1. Do you have in Corvid any library to interact with JSON-LD?

and, 3. Do you know if there are wix specialized partners in developing semantic web interfaces?

This post is from 2018 and being closed.
Please add a link to refer back to this post instead of bumping up an old post.

As for your questions.

  1. No not that I am aware of, you can try adding external datasets as shown here or expose your site to the RDF API or get the data in XML form and bring that into Wix.
    https://support.wix.com/en/article/corvid-working-with-external-database-collections
    https://support.wix.com/en/article/corvid-adding-and-deleting-an-external-database-collection
    https://www.wix.com/corvid/reference/external-database-collections.html
    http://wifo5-03.informatik.uni-mannheim.de/bizer/rdfapi/tests.html
    https://www.w3.org/TR/rdf-sparql-XMLres/

You can use a nodeJS in Wix Package Manager to do XML to JSON.
https://www.wix.com/corvid/forum/community-discussion/parse-xml-string-from-fetch-api
https://www.wix.com/corvid/forum/community-discussion/get-xml-response
https://support.wix.com/en/article/corvid-managing-external-code-libraries-with-the-package-manager

  1. https://support.wix.com/en/article/adding-structured-data-to-your-site
    https://support.wix.com/en/article/troubleshooting-your-structured-data

  2. Not that I am aware of unless you go asking in Wix Arena or Wix Partners.
    Although, if you want to build a semantic web of data, then you can and just add it to your Wix site through external datasets or exposing your Wix site to a API to connect your Wix site to your stored data, as already mentioned in 1.