0

This is probably a simple solution, however I cannot figure out how to carry my JSON object over to my view. Normally you'd throw JSON into res.render() inside your app.get .. unfortunately I am having trouble. A problem arises because i need to be able to send form data from html to my API request, then grab that JSON object and display it in my view. I can see it in my console, but unable to carry it over to html. Would love some help or guidance on how to improve my code or find a solution. Any help is appreciated! - View code below:

Server.js

var path = require('path');
var express = require('express');
var exphbs  = require('express-handlebars');
var bodyParser = require('body-parser');
var Bls2 = require('bls2');

var app = express();

app.engine('html', exphbs({ extname: '.html' }));
app.set('view engine', 'html');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var blsRequest = function(req, res, inputContent){
    const API_KEY = //bls API key here
    let bls = new Bls2(API_KEY);
    console.log('this is seriesID =', inputContent);

    let options = {
        'seriesid': [inputContent],
        'startyear': '2008',  
        'endyear': '2018',
        // ...
    };

    bls.fetch(options).then(function (response) {
        var data = JSON.stringify(response)
        console.log(data);
        console.log('API Call Complete')

        res.send({data : data}); //need to render home view and JSON HERE
    });
}

app.get('/', function (req, res) {

    res.render('home');
});

app.post('/', function(req, res){
    let inputContent = req.body.seriesID;
    blsRequest(req, res, inputContent);

});

app.listen(8000);

html

<!DOCTYPE html>
<html>
<head>
    <title>LBS</title>
    <meta charset="utf-8">
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="crossorigin="anonymous"></script>
</head>
<body>
    <div class="container-fluid">
        <form id="seriesID">
            <div class="form-group" method="post" action='/'>
                <label for="seriesID">Input Series ID</label>
                <input type="text" class="form-control" name="seriesID" placeholder="Series ID">
            </div>
            <button class="btn btn-primary" type="submit"  >Search</button>
        </form><br>

        uhh JSON should be here: {{data}}
    </div>
</body>
    <script type="text/javascript">
        $("#seriesID").submit(function (event) {
            $.post('/', $("#seriesID").serialize(), function (data) {
                 //data is the response from the backend
            });
            event.preventDefault();
        });
    </script>
</html>
kid0m
  • 1
  • 1
  • Where you have your res.send, replace it with `res.render('home', {data})` – dotconnor Sep 29 '18 at 05:04
  • Check this https://stackoverflow.com/questions/21843840/what-does-res-render-do-and-what-does-the-html-file-look-like – Linoy Sep 29 '18 at 05:08
  • i have tried both `res.render('home', {data})` AND `res.render('home', {data : data})`, neither work – kid0m Sep 29 '18 at 23:28

1 Answers1

0

use res.render('home',{data}); because your{{data}} is undefined that's why it is not printing anything it is even better to use {{JSON.stringify(data)}}

  • i have tried both `res.render('home', {data})` AND `res.render('home', {data : data})`, neither work. Also I am console logging data. i can see the object in console, i simply cannot carry it over to html – kid0m Sep 29 '18 at 23:30