Is there any way to convert an address user input to a string?

So, I’m trying to take the address from an address user input and convert it to a string so that I can enter it into a text field (txtAddress) in a collection (Addresses) as well as enter the original address into an address field (addAddress) in the same collection.
I’m doing this so that I can search the database by the text address because I can search by a text field, unlike an address field.
I don’t know what’s wrong with the code, but any help would be greatly appreciated.

export function btnSubmit_click(event) {
//Add your code for this event here:
let street = $w( “#inptAddresses” ).value.formatted;
let toInsert = {
“txtAddress” : street,
“addAddress” : $w( “#inptAddresses” ).value,
“dateOfInstall” : $w( ‘#inptDateOfInstall’ ).value,
};

wixData.insert($w( '#AddressesDataset' ), toInsert) 
	.then( (results) => { 
 		**let**  item = results;  //see item below 
		} ) 
	. **catch** ( (err) => { 
		**let**  errorMsg = err; 
		} ); 

}

Also, let me know if there’s anything else that would help in figuring this out

I’m not sure you can insert by referencing the page element:

wixData.insert($w('#AddressesDataset'), toInsert)

Instead you should use the collection name i.e. where your collections is called Addresses then the code should be:

wixData.insert("Addresses", toInsert)

You will also need to ensure that you have write permissions to this database. If you need to keep the permissions restricted then you will probably be best writing a backend function and using suppressAuth to bypass the permissions.

I have all of the permissions set to everyone and I’ve tried using the collection name instead of referencing dataset but I get this error;

WDE0026: The Addresses collection was removed, so you cannot work with it. To restore its data, create a new collection with the same name.

when the code is changed to;

wixData.insert("Addresses", toInsert)

and the collection is;

Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor. Please include the name of the page involved.

https://editor.wix.com/html/editor/web/renderer/edit/c3ce1a2e-10be-429f-a16c-f968c015b8c5?metaSiteId=55d3d8d0-a4d9-41ff-95e6-ed0dbd262842&editorSessionId=0a00874a-9cdc-4b89-b999-754db948fd25&referralInfo=dashboard

the “Add New Address” page

Sounds to me like you have previously created an Addresses collection and deleted it. Then created a new one. You need to find the collection ID which you can do by going to the collection settings in Content Manager. I imagine your collection may be start with a lower case “a” or something like that.

Once you have the ID, you should use this in your code

@iaindowner awesome, thanks. The database collection name is working now, but the code is creating 2 entries in the collection, the first has the date of access and original address, but nothing in the text address field, and the second one is blank.

You have your Submit button connected to a Dataset that is Write-only:

Plus, you have the Submit button connected to a click event handler which creates a conflict with the button being a Submit button.

You should either use a regular button with a click event handler which will do the wixData.insert, or connect your Submit button to a read-write dataset and don’t connect to a click event handler.

@s_wilko Regarding the two entries, I think maybe this is becuase you are submitting to the collection in two manners:

  1. You have written the above code which inserts on click

  2. Your submit button is linked to the dataset element

Both of these will run and cause the issue.

I would recommend to remove the submit button link

This should hopefully also resolve your issue with the address text field not being populated but let us know if there is still an issue.

@iaindowner Thanks so much, that’s really obvious now that you mentioned it. I made it so the submit button isn’t connected to the dataset element, however the text address field still isn’t being filled with a text version of the original address, is there any way to fix this?

I’ve changed it so the submit button isn’t connected to the dataset element, but instead just working through corvid and it seems to be connected to the collection fine now, but the text address field still isn’t being filled with a text version of the original address even though the date of install and the original address field are both being filled, do you know any way to fix this or to do it differently?

@s_wilko I think you need to store the value as a variable first and then access the propertis maybe. So something like the following:

Instead of:

let street = $w("#inptAddresses").value.formatted;
let toInsert = {
    "txtAddress": street,
"addAddress": $w("#inptAddresses").value,
"dateOfInstall": $w('#inptDateOfInstall').value,
};

Use:

let address = $w("#inptAddresses").value
let street = address.formatted;
let toInsert = {
"txtAddress": street,
"addAddress": $w("#inptAddresses").value,
"dateOfInstall": $w('#inptDateOfInstall').value
};

Let me know if that doesn’t work

@iaindowner It works! Thank you so much! I put in what you recommended and I also checked the field names and it turns out that the “txtAddress” field should have been “txtAddresses,” so it now works.

Well, what’s happening is that the address is being saved as, well, as an address. That may sound stupid, but there’s actually real meaning behind such an innocuous statement. If we look at your database collection, this is what we see:

What you’ll need to do is to get the address field from the Collection and then use it as an Address object. If you want to display an address string (e.g. formatted string), then you’ll need to do something like this:

Let’s say that addressField is from the address field in the collection, it’s from the Address input, you will need to get the formatted string for display like this:

let address = $w("#myAddressInput").value; // as an example
let formattedString = address.formatted;
$w("#text1").value = formattedString;

The above (non-tested) code should display the formatted address in the #text1 text box.

For more information about what an address looks like, see the Address API.