Thursday, 14 February 2019

ElasticSearch + Node: search button must be clicked three times

I am working on a react.js + Node + ElasticSearch project and I have managed to get the search to work! however, I have to click the search button three times before I get back results in my console. any input would be great!import React, { Component } from 'react'; import axios from 'axios'; class App extends Component { state = { result: [], name: 'Roger', userInput: null, } handleSubmit = event=> { event.preventDefault(); var input = document.getElementById("userText").value; this.setState({ userInput: input }); axios.get('http://localhost:4000/search?query=' + this.state.userInput) .then(res => { var result = res.data; this.setState({ result: result }); }) console.log(this.state.result); console.log(this.state.userInput); } render() { return (

hello from react

); } } export default App; Here is Node:const express = require('express'); const bodyParser = require('body-parser'); const morgan = require('morgan'); const JSON = require('circular-json'); const PORT = 4000; var client = require ('./connection.js'); var argv = require('yargs').argv; var getJSON = require('get-json'); const cors = require('cors'); let app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cors({ origin: 'http://localhost:3001', credentials: true })); app.get('/', function(req, res){ res.send("Node is running brother"); }); app.get("/search", function (request, response) { client.search({ index: 'club', type: 'clubinfo', body: { query: { match: { "name": query} }, } },function (error, data, status) { if (error) { return console.log(error); } else { // Send back the response response.send(data); } }); }); app.listen(PORT, () => console.log('wowzers in me trousers, Listening on port ' + PORT)); ​

Submitted February 14, 2019 at 11:38PM by CharlieCannedFruit

No comments:

Post a Comment