0

I'm working on a VueJS web app, in which I need to query a db of peoples names based off user input.


In my server.js file I have my API endpoint which I want to query and have it hosted on localhost:4000

//Staff
app.get('/staff', (req,res) => {
    connection.query(SELECT_CERTAIN_STAFF, (error,results) => {
        if(error){
            return res.send(error)
        }
        else {
            console.log('Selected STAFF from People')
            return res.json({             
                data: results
            })
        }
    })

})

In my Search.vue this is my search method

  //Data
  data(){
    return {
      input: '',
      errors: ''
    }
  },

  //Methods
  methods:{
    search(keyboard){
      console.log(this.input)
      axios.get('http://localhost:4000/staff?username='+this.input)
      .then(response => {
        console.log(JSON.stringify(response.data.data))
      })
      .catch(error => {
        console.log(error)
        this.errors = error
      })
      console.log(keyboard.value)
    }
  },

I added console.log(this.input) + console.log(keyboard.value) to test the correct input is being taken from the user, which it is

In my response, the console.log(JSON.stringify(response.data.data)) is just returning the data in the endpoint /staff, and is not filtering any of the data based on user input.

Anyone have any idea why it's doing this/ different approach? Have I set up the API endpoints correctly?

Thanks

Tom
  • 83
  • 2
  • 11
  • What is the value of `SELECT_CERTAIN_STAFF` in your server.js? The question seems to be more about your serverside filtering than about Vue, so you might want to adjust your tags. – padarom Nov 13 '18 at 09:43
  • It's my query for selecting all relevant staff members in the db const SELECT_CERTAIN_STAFF = "SELECT people.username, people.firstnames, people.surname, people.email, staff.office, staff.phone FROM people INNER JOIN staff on people.username = staff.username WHERE staff.status IN ('academic', 'prof', 'ra', 'support') AND active ='1'" Do I need to set up a new query? – Tom Nov 13 '18 at 09:45
  • That depends on whether you want the filtering to take place in the database request or in your application layer. Either you need to add a separate `WHERE` clause for the username if it was present in the request, or you need to filter the results afterwards based on the provided username. – padarom Nov 13 '18 at 09:48
  • I guess I was trying to filter the results afterwards but it hasn't worked for me. How would the WHERE clause look? ie transferring the value from the user into the server side query Thanks – Tom Nov 13 '18 at 09:51
  • @Padarom Something like this? https://stackoverflow.com/questions/41168942/how-to-input-a-nodejs-variable-into-an-sql-query --- Unsure how to put the user input into that query field – Tom Nov 13 '18 at 10:49

0 Answers0