MBBertolucci
MBBertolucci

Reputation: 1341

How to pass a variable from Express to Jade

I am using IBM Bluemix to make a web service for a school project.

I need to pass some variables from the .js to the .jade file, but I am having trouble in doing so.

I know I have to declare an object which hold the variables and then use

#{Variable}

in the Jade Template, but I can't figure out why this method is not working on my code.

What could be the cause of this problem and how is it possible to solve it?

Here is my .js code:

/*eslint-env node*/

//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------

// HTTP request - duas alternativas
var http = require('http');
var request = require('request');

// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');

//chama o express, que abre o servidor
var express = require('express');

// create a new express server 
var app = express();

// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));

// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
    // print a message when the server starts listening
    console.log("server starting on " + appEnv.url);
});


app.get('/home1', function (req,res) {
    http.get('http://developers.agenciaideias.com.br/cotacoes/json', function (res2) {
        var body = '';
        res2.on('data', function (chunk) {
            body += chunk;
        });
        res2.on('end', function () {
            var json = JSON.parse(body);
            var cotacao = json["bovespa"]["cotacao"];
            var UsdValue = json["dolar"]["cotacao"];
            var UsdVariation = json["dolar"]["variacao"];
            var CotacaoEuro = json["euro"]["cotacao"];
            var VariacaoEuro = json["euro"]["variacao"];
            var TimeOfUpdate = json["atualizacao"];
			
			res.render('cotacao_response.jade', {
                 'UsdValue':UsdValue,
				 'TimeOfUpdate':TimeOfUpdate,
				 'UsdVariation':UsdVariation
            });
        });
    });
});

Here is my .jade code:

doctype html
html(lang="en")
  head
    title Cotação 
    link(rel='stylesheet',href='stylesheets/style.css')
  body
  h1 Your exchange rate is #{UsdValue}. 
  h1 The variation was #{UsdVariation}%.
  p The values were updated in !{TimeOfUpdate}.
  #container.col
  p 
  p.
        

Upvotes: 0

Views: 874

Answers (1)

marukobotto
marukobotto

Reputation: 771

You didn't set it in the beginning. It makes it more easier

app.set('view engine', 'jade');

And while rendering:

res.render('cotacao_response', {
   'UsdValue':UsdValue,
   'TimeOfUpdate':TimeOfUpdate,
   'UsdVariation':UsdVariation
});

And for god's sake. Use another templating engine. Jade is very disturbing from my point of view due to indentation and 1-space stuff. It eats up a lot of time if some minor error happens, and you have to do that once again from top to bottom. Who likes that?

Upvotes: 3

Related Questions