I created a database that has the details of users and I want it to be displayed my VueJS by searching its first name.
Below is my code to request details of the users in a JSON. It's working so my next step would be display the data in my VueJS.
http://localhost:9000/api/user/:id
I got something like this on my VueJS
export default {
data() {
return {
search: "",
results: {}
};
},
mounted() {
this.getUsers();
},
methods: {
getUser() {
axios
.get("http://localhost:9000/api/user/")
.then(response => (this.results = response.data))
.catch(error => alert(error));
}
},
computed: {
/* Search Bar */
filteredPeople() {
if (this.search) {
return this.results.filter(result => {
return result.First_Name.toLowerCase().startsWith(
this.search.toLowerCase()
);
});
} else {
return this.results;
}
}
},
};
</script>
and to display it, I created something like this
<div v-for="result in results" :key="result.id">
<input type="text" v-model="search" placeholder="Search by name" />
{{result.First_Name}}
{{result.Last_Name}}
</div>
Here's my nodejs query to request a specific user
//gets a specific user based on their ID
router.get('/user/:id', function (req, res) {
let sql = "SELECT * FROM MEMB WHERE id = ?";
myDB.query(sql, req.params.id, function (err, rows) {
if (err) {
console.log("Your query has error."+ err);
} else {
console.log(rows);
res.send(rows);
}
});
});