I’m trying to code a simple order history section for my member’s section (coded, not the Wix app) and need to reference a permission field in a database to do so.
How can I properly reference a specific field from a database? (Please keep I’m a beginner and looking at the API page left me feeling more confused than enlightened)
I assume you have Wix Code turned on since you coded a members section which is nice.
To query a database you need to import the correct library for it which is:
import wixData from 'wix-data';
(Put this at the top of your code)
Next find the $w.onReady() function and insert this code to query your database:
wixData.query("myCollection") //Enter your database name instead of myCollection
.find() //Finds all items in the database
.then( (results) => {
let items = results.items; //here are all your items in that database
let permissionFieldItem = items[0].permissionField; //Assuming your specific field is called permissionField, this would get it
let firstItem = items[0];
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
I entered the code but I’m running into a little trouble. I needed to query the database so that I could hide a picture link on a member’s page if they don’t have access. I’ve labeled people who have access with ‘Yes’ for the field I’m querying.
Here’s what I have so far:
Database: Members
Field: ZR1purch
Label in field to allow access: Yes
Picture Link: zrpr01
import wixData from ‘wix-data’;
$w.onReady( function () {
wixData.query(“Members”) //Enter your database name instead of myCollection
.find() //Finds all items in the database
.then( (results) => { let items = results.items; //here are all your items in that database let ZR1purch = items[0].ZR1purch; //Assuming your specific field is called permissionField, this would get it let firstItem = items[0];
})
. catch ( (error) => { let errorMsg = error.message; let code = error.code;
}); if (items[0].ZR1purch) contains(“Yes”){
$w(“zrpr01”).show();
} else {
$w(“#zrpr01”).hide();
}
With those requirements in mind I can give you the steps and code for your problem,
First you have to get the user id of the current user, then you will have to query the members database with this id and find the member. After, you will have to check if his ‘ZR1purch’ field is equal to yes, if it is show, else hide.
Here is some code, but I recommend using the wix api reference and trying to do it alone first:
import wixUsers from 'wix-users';
let user = wixUsers.currentUser;
let userId = user.id; //get the user id to lookup in database
wixData.query("Members")
.eq("id", userId) //we query the user id we got above and find it in database and get back all the data for this user
.find()
.then( (results) => {
console.log(results); //This will show all the information for that item in the database
if(results.items[0].ZR1purch === 'Yes') { //Check to see if this field is yes
$w("zrpr01").show();
}else {
$w("zrpr01").hide();
}
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
console.log(errorMsg, code); //This will show the error in your console if there is one
});
I was able to get the code to work one I realized I forgot to include the ‘#’ symbol when referencing the zrpr01 item! I also added a underscore to “id”.
Here’s what the final code looks like!
import wixData from 'wix-data';
import wixUsers from 'wix-users';
let user = wixUsers.currentUser;
let userId = user.id;
$w.onReady(function () {
wixData.query("Members")
.eq("_id", userId)
.find()
.then( (results) => {
console.log(results);
if(results.items[0].ZR1purch === 'Yes') {
$w("#zrpr01").show();
}else {
$w("#zrpr01").hide();
}
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
console.log(errorMsg, code);
});//TODO: write your page related code here...
});
Try going into your database and look for this field, after you find it check its properties to make sure its field key is the same as you are writing it. If this isn’t the problem try console logging that field after you get it on your page to see what the value is.