Shehzad Lokhandwalla Shehzad Lokhandwalla - 1 year ago 89
Javascript Question

How to pass information to view from controller in NODE JS Express

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:


Which has two forms

form#form_search_book(name="search", method="post", action="/searchbook")
span ISBN
input#input_isbn(type="text", placeholder="isbn", name="isbn")
button#search_book(type="search") search
form#form_add_book(name="addbook", method="post", action="/addbook")
span Book Name
input#input_name(type="text", placeholder="book name", name="book_name")

And I have the NodeJS controller:
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' });'/searchbook', function(req, res){
var isbn=req.body.isbn;
var url=""+isbn;
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(\[0\].isbn13) // Print the json response


Link to the view

Answer Source

I think you got confused about what code's being run in the client-side and what code run in server-side. This code works only on a browser not in NodeJS.


you need to pass your values through a querystring in redirect method.

I presume you can find your answer in this post : How do I redirect in expressjs while passing some context?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download