I'm quite new to NodeJS. I'm trying to create a form that accepts several inputs. One of the functions of the form is to take in an input--> ISBN and automatically get information from an API and add it in the input fields. However, I cannot pass information from the controller to the view.
I have a view: newbook.jade
Which has two forms
form#form_search_book(name="search", method="post", action="/searchbook")
span ISBN
span
input#input_isbn(type="text", placeholder="isbn", name="isbn")
button#search_book(type="search") search
br
br
form#form_add_book(name="addbook", method="post", action="/addbook")
span Book Name
span
input#input_name(type="text", placeholder="book name", name="book_name")
And I have the NodeJS controller: index.js
that executes the /searchbook form and then redirects back the /newbook page. When it redirects to the new book page, the information from the API should be passed on to the input form...but I've been struggling on how to do that.
Any help would be appreciated.
router.get('/newbook', function(req, res){
res.render('newbook', {title: 'Add New Book' });
router.post('/searchbook', function(req, res){
var isbn=req.body.isbn;
var url="http://isbndb.com/api/v2/json/63JEP95R/book/"+isbn;
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body.data\[0\].isbn13) // Print the json response
}
});
res.redirect('/newbook');
document.getElementById('input_name').value=body.data\[0\].title;
document.getElementById('input_isbn').value=body.data\[0\].isbn13;
});
});