I am working on a project, where I am utilizing the Google Maps API, to autocomplete the address for me.
API DOCS: https://developers.google.com/maps/documentation/javascript/places-autocomplete
The response I am getting from the API is somewhat similar to this
(5) [{…}, {…}, {…}, {…}, {…}]
0: {long_name: '15', short_name: '15', types: Array(1)}
1: {long_name: 'Endrupvej', short_name: 'Endrupvej', types: Array(1)}
2: {long_name: 'Fredensborg', short_name: 'Fredensborg', types: Array(2)}
3: {long_name: 'Danmark', short_name: 'DK', types: Array(2)}
4: {long_name: '3480', short_name: '3480', types: Array(1)}
length: 5
[[Prototype]]: Array(0)
The reason I am using this API is to make sure no mistakes is made when registrering on my website.
I am trying to get the 4th index in the object with following code.
document.getElementById("zip_code").value = place.address_components[4].long_name;
This works fine, the problem for me comes when I try to parse this into my SQL statement, as I get the following error
message: "Invalid column name 'undefined'.",
const createUser = (req, res) => {
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
let { name, surname, email, address, zip } = req.body;
let id = uuidv4();
let userPassword = req.body.password;
let hashedPassword = bcrypt.hashSync(userPassword, saltRounds);
// query to the database and get the records
request.query(
`insert into users(User_ID, User_firstName, User_surname, User_email, User_password, User_address, User_zip) VALUES ('${id}','${name}','${surname}','${email}','${hashedPassword}','${address}',${zip})`,
function (err, recordset) {
if (err) {
console.log(err);
} else {
console.log("User added to DB");
}
}
);
});
res.redirect("/login");
};
<form action="/createUser" method="POST" class="py-5 col-6">
<input
type="text"
class="form-control"
id="zip_code"
name="zip"
placeholder="Adresse.."
required
disabled
/>
</form>
Hopefully somebody can help me figuring this out!