i’m trying to retrieve a single value from database in a text box, but all i can do i retrieving a whole field in the dynamic field or whole category in dynamic category, i’m trying to retrieve just one value from a specific field and a specific row.
thank you in advance
Did you had a look to the Wix Code API Reference already?
https://www.wix.com/code/reference/wix-data.html#query
You could query your collection for a specific row and pass the specific field of the received collection-item to your text-box.
Maybe you want to post here your code you already have?
thank you very much Tom for your help, i took a look at the query function and added the code:
import wixData from ‘wix-data’;
// … wixData.query(“myCollection”)
.find()
.then( (results) => {
let firstItem = results.items[0]; //see item below } )
.catch( (err) => {
let errorMsg = err;
} );
now i can retrieve the value to a text box after i connected the text box to the wanted field,
now i need to add an if statement, that would determine another value in another text box,
how can i define a specific value (specific field and specific row) so i can use if statement with it,
if (the specific field and row) = 2
text1.text = 10
can you help me with that?
Hi,
lets try this.
wixData.query("myCollection")
.find()
.then((result) => {
if (result.items[0].specificField == 2) { // Check if field holds expected value
$w("#text1").text = "10" // Set the element text to desired value
}
})
Now, this does not select a specific row. For selecting a row you may have an indicator in the row (another field with a specific value), then you would fetch the row the following way:
wixData.query("myCollection")
.eq("indicator", "matchingValue")
.find()
.then( ... )
I hope this helps. Let me know if it did or you need any further assistance. And have fun Wix Coding!
thank you Giedrius i’ll try it
How to show database value of employee name into text field, using input element of ID No and submit button
Input-field = ID → Output-Field = Name?
Ok, you have 1x input-field and 1x output-field (‘#myInput’ / “#myOutput”).
-
First you get the value of —> ‘#myInput’
$w.onReady(()=>{
$w(‘#myInput’).onChange(()=>{
let myValue = $w(‘#myInput’).value
});
}); -
Now you have inserted your wished ID into the input —> myValue=any-ID.
-
Now take a look on this one…
https://www.wix.com/velo/reference/wix-users-backend/getuser
Here you will get the right data you are searching for. -
Last step would be to create the output, after you have send back the result from back-end, to frond-end.
let myResult = await (myBackendFunction(ID)).
- As you can see, you will need to create a back-end-JSW-file.
on providing employee id and clicking view, i should get name and designation of employee in respective text field, Hope you can sort this to me.
Thanks in Advance.
- Create a JSW-backend-file.
- Give it the following name —> getMemberData.jsw
- Put this code onto your front-end —> PAGE-CODE:
import {getMemberDetails} from 'backend/getMemberData.jsw'
var myValue
$w.onReady(()=>{
$w('#myInputIDhere').onChange(()=>{setTimeout(()=>{let myValue = $w('#myInputIDhere').value},50);});
$w('#myInputIDhere').onChange(async()=>{let mySearchedValue = await getMemberDetails()});
});
- Put this code onto your backend-code…(getMemberData.jsw)
import wixUsersBackend from 'wix-users-backend';
export function getUser(ID) {
return wixUsersBackend.getUser(ID)
.then((user) => {
return user;
});
}
EDIT: Change → getUser → to → getMemberDetails
export function getMemberDetails(ID) {.......
Expand the following code-part…
$w('#myInputIDhere').onChange(async()=>{
let mySearchedValue = await getMemberDetails()});
$w('#myTextFieldIDhere').text = mySearchedValue.name
});
and tanother fix… it should be onClick of course for your button…
$w('#myButtonIDhere').onClick(async()=>{
let mySearchedValue = await getMemberDetails(myValue)});
$w('#myTextFieldIDhere').text = mySearchedValue.name
});
Similar situation here…
https://www.wix.com/velo/forum/coding-with-velo/input-value-redirect-1
Thanks for your help, but we are not pointing our collection,then how can it show data of user with employee ID
My idea is
I will be creating collecton o employees, on searching with id for employee name and desgnaion, it should point my collecion ( Employee Records) and provide me name & designation
@vishnu8b Did you have read this post?
https://www.wix.com/velo/forum/coding-with-velo/input-value-redirect-1
There you will find a similar functionality which almost fits your needs.
- You have a database → “Empleyee Records”, where you store all your EMPLOYERS
- You have an INPUT-Field where you insert an “EMPLOYERS-ID”.
- You click on a button to start the search-engine.
- The search-engine takes a look into your EMPLOYEERS-DB and find the right -->ID
- And gives you as RESULT the right name of the searched EMPLOYEER in your DB
Isn’t this the way your script should work ?
If so, then the shown example above is almost exactly what you need.
All you will have to do is to → change <— inputDATAFIELD / outputDATAFIELD & DATABASE reference-values .
Instead of —> “index” <— you want to search for —> “_id” → input
Instead of —> " url" <— you want to be the —> “name” as your output.
Instead of —> “TEST-DB” <— you insert the id of your own DB → “Employee Records”
import wixData from 'wix-data';
import wixLocation from'wix-location';
//------User-Interface---------
var DATABASE = "TEST-DB" //<---- put in here the ID of your new DB !
var inputDATAFIELD = "index" //<-- put in here the ID of your input- DATAFIELD (INDEX)
var outputDATAFIELD = "url" //<-- This will be the output ---> URL
//------User-Interface---------
var myValue
$w.onReady(()=>{
$w('#input1').onChange(()=>{
myValue = $w('#input1').value
console.log("My value "+ myValue +" setted!")
});
$w('#button1').onClick(()=>{console.log("Button-1 clicked!")
searchIndex(myValue)
});
});
function searchIndex(VALUE){console.log("Searchfunction started. Value = ", VALUE)
wixData.query(DATABASE)
.eq(inputDATAFIELD, VALUE)
.find()
.then(async(results) => {
let items = await results.items; console.log(items)
if(items.length > 0) {console.log("Item found! Redirection starts in 3-secs.")
let myURL = await results.items[0][outputDATAFIELD]; console.log("Item-URL: ", myURL)
wixLocation.to(myURL)
}else {console.log("No matching data found in DB.")}
})
}
I completed my code dude.
Anyway Thanks for your Concern and sharing your knowledge with me.
Once again Thank you Buddy.