Joss
Joss

Reputation: 555

Pass vars with Nodejs from controller to view not working

I'm not able to pass a var from a controller to a view with Nodejs.

Currently, I'm using express, and this is my route definition

app.get('/search/:id', function(req,res){
    searchController.findByName(req, res);
    // console.log(res);                        //--->Check res is right
    res.render('search', { test: 'test' });
});

searchController has this code:

var mongoose = require('mongoose');
var search = mongoose.model('products');

exports.findByName=function(req, res){
    var regex = new RegExp("^"+req.params.id, 'i');
    search.find(
        {'name': regex}, function(err, result) {
        if(err) return res.send(500, err.message);
        res.status(200).json(result);
    });
};

Connection to mongoDB is right using mongoose. If I console.log(res) in my controller, I obtain a quite hudge JSON file (more than 500 lines!), but I can't find the results of the query to the database.

I take an empty array from the database, I've checked with Postman. This means, problem is not in the database:

enter image description here

Is it normal to have this so long JSON file? I know I can send data to the view with the second argument How could I pass the query from the controler to the view with res.render('search', { test: 'test' });, but test is also unknown in the view, no matter if I also write res.render('search', { 'test': 'test' });

Thanks!

** UPDATE **

Thanks to Mike's answer I've refactored my code to the following:

** Route **

app.get('/search/:id', function(req,res){
    searchController.findByName(req, res);
});

** Controller **

exports.findByName=function(req, res){
    var regex = new RegExp("^"+req.params.id, 'i');
    search.find(
        {'name': regex}, function(err, result) {
        if(err) return res.send(500, err.message);
        console.log(result);  //--> NEW console.log inside controller, works
        res.render('search', {'result': result});
    });
};

Now result is an empty array (which is the right value from the database) but still is not rendering this var in the view.

** VIEW FILES **

search.jade

include search.php

search.php

<script>
     var productsJSON = result;
</script>

** SECOND UPDATE **

Thanks to Mike's answer (again), I'm doing some progress. Now my contoller is:

exports.findByName=function(req, res){
    var regex = new RegExp("^"+req.params.id, 'i');
    search.find(
        {'name': regex}, function(err, result) {
        if(err) return res.send(500, err.message);  
        res.render('search', {'pageData': {'result' : result}});
    });
};

I've added '' to pageData because I think must be there.

Now my views are:

search.jade

include header.php

script.
  var productsJSON = #{pageData.result}

But is not working, I've the following error:

enter image description here

I've tried to follow some of the tricks shown in this page but is not working at all.

Upvotes: 0

Views: 1951

Answers (1)

Mike
Mike

Reputation: 11545

You are responding twice when this endpoint is hit. You are sending a response with res.render('search', { test: 'test' }); and also with res.status(200).json(result);.

Try moving res.render('search', { test: 'test' }); in place of res.status(200).json(result);. You will then only have one chance of responding from this endpoint.

The large JSON response object is correct. That is what the response object looks like. It should include a bunch of stuff like it does in your example above.

To display your data in the Jade template you will need to use the following syntax as described in this answer.

Respond like this:
res.render('search', {pageData: {'result' : result}});

Then in your template render the value like this:
#{pageData.result}

You need to wrap any variables passed from the server side in #{} if you want them to display in the template.

Upvotes: 1

Related Questions