Hi…
I’m tring to get de value of a var inside .then, but i don’t know how.
I made this:
var NombreDis = "";
wixData.query("Distribuidores")
.include("title", "correo")
.eq("correo", Email)
.find()
.then((num) => {
let numberOfItems = num.totalCount;
let Items = num.items;
let Registro = Items[0];
let dNombre = Registro.title;
NombreDis=dNombre;
});
$w("#text56").text = NombreDis;
but the “text56” does not receive the value and the “text56” apears empty…
How can I get the value from “dNombre” and can use it in another parts?
Thanks
The problem work the code is that when you see .then, it means something will happen later. The party where you use the variable actually happens before you get the value from the database.
Just remove the var, and set the value to the text from inside the .then callback
Thanks…
Yes, I Use - $w(“#text56”).text = NombreDis; - inside the .then and works fine…
but that part is just an example, becouse that part is just one of many lines where I need use it…
So, please helpme please to use the value of “dNombre” outside of .then
Thanks
You can set the value to the variable outside of the .then. however, it will happen later because .then happens later (e.g. async)
Your will need to trigger all it usages from the .then callback
Hi, I am facing the same problem. I have a query and I need the value of a specific column (Field).
How to do that?
In my case I have the following code to get member’s email:
let userId = wixUsers.currentUser.id;
console.log("ID: "+userId);
wixData.query("Members")
.eq("_Id", userId)
.find()
.then((results)=>{
let Email = results.items["Email"];
console.log("email: "+Email); //I'm getting Email undefined
});
Thank you in advance!
Hi Ricardo,
Try this code in .then
See my comments
let Email = results.items[0]["Email"]; // Email should match the field key in the Database collection view. (in Database view click on the three dots and pick manage properties and you will see the field key)
Good luck!
Hi Roi,
thanks for the reply!
I made the changes and checked that the field key on Members database is “Email” indeed, but I’m still receiving “Email: undefined”. I don’t understand what is going on.
I share with you the code below. My website is: www.impacttrack.org
Any help is very welcome!
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
NewProject();
});
export function NewProject() {
//Add your code for this event here:
let userId = wixUsers.currentUser.id;
console.log("ID: " + userId);
///get user email
wixData.query("Members")
.eq("_Id", userId)
.find()
.then((results) => {
let Email = results.items[0]["Email"];
console.log("email: " +Email);
{wixData.query("ProjectPage")
.eq("_Id", userId)
.find()
.then((result) => {
console.log("result: "+result);
// if an item for the user is not found
if (result.items.length === 0) {
// create an item
const toInsert = {
"title": "Add new venture",
"Email": Email,
"_owner": userId
};
console.log("insert: "+toInsert);
// add the item to the collection
wixData.insert("ProjectPage", toInsert)
.then((results))
.catch((err) => {
console.log(err);
});
}
} );
}
});
}
Hi Ricardo,
I looked into your code,
Wix users and your Members collection are not related other than semantics.
In order to get the user email you can use the method “getEmail”
Check this out:
https://www.wix.com/code/reference/wix-users.User.html#getEmail
Good luck!
Hi Roi,
thanks again for your reply. The “getEmail” method is not working for me.
I have built a member profile page (following the instructions below), which extract the email and id from the user and insert at the “Members” collection. This code is at the site level and it is working properly.
My site aims to allow users to create and manage several project pages. In order to do that, I created a page (Portfolio) that shows a table with all user’s projects recorded at “ProjectPage” collection. The Portfolio page is a dynamic page and uses email in its URL.
The user should go from the Profile page to the Portfolio, however, it only works if there is its email at “Project Page” collection. So, what I wanted to do is: extract user’s email from “Members” collection, search whether it exist at “ProjectPage” collection and if it exists, insert a record with user’s email. Everything seems to be working well, except the email search at “Members” collection.
Best wishes, Ricardo
Hi,
Can you please share a link to your site?
Roi Bendet
Hi Ricardo,
This is your code from Members dynamic page:
Check out my comments
wixData.query("Members")
.eq("_Id", userId) // "_Id" should be "_id". field key is case sensitive
.find()
.then((results) => {
let Email = results.items[0]["Email"];
console.log("email: " + Email);
{ // delete this
wixData.query("ProjectPage")
.eq("_Id", userId) //"_Id" should be "_id".
.find()
.then((result) => {
console.log("result: " + result);
// if an item for the user is not found
if (result.items.length === 0) {
// create an item
const toInsert = {
"title": "Add new venture",
"Email": Email,
"_owner": userId
};
console.log("insert: " + toInsert);
// add the item to the collection
wixData.insert("ProjectPage", toInsert)
.then((results))
.catch((err) => {
console.log(err);
});
}
});
} // delete here as well
});
Good luck!
Let me know how it goes
Roi
Thanks Roi!
It works now!