0

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);
        }
    });
});

2 Answers 2

1

Try to change getUser() like that:

getUser() {
  let self = this;
  axios
    .get("http://localhost:9000/api/user/", {
      params: {
        id: 12345
      }
    })
    .then(response => (self.results = response.data))
    .catch(error => alert(error));
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Vin, what console.log(response.data) returns ?
None, idk why. Maybe I need to pass a data .get("localhost:9000/api/user" + id)? Something like that
@Vin, try to add the id like I did just now.
0

Just a work around to this:

Please check this out: https://bootstrap-vue.js.org/docs/components/table#complete-example

It has got ready functions for bootstrap-vue to search by Name. All you have to do is:

  1. Put a boostrap-vue table
  2. Return an array of users from API to axios response
  3. Set items to array of users

2 Comments

Can this table handle big data?
Yes. I tested it on 10000 records from mongo db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.