On Jan 28, 2014, at 19:39, Anto <
pot...@gmail.com> wrote:
> The code is as follows:
>
> - Nodejs + Expressjs
>
> app.get('/brand/:identifier', function(req, res){
>
> identifier = req.params.identifier.toLowerCase();
Because you have not used “var” to declare it, “identifier” is a global variable. This is a problem. Check your other code for this problem as well.
> brands.find({ active: true, 'brandName' : { $regex : new RegExp(identifier, "i")} }).populate({
> path: 'shop'
> ,select: 'name shop description'
> }).sort({dateAdded: 'desc'}).exec(function(err, doc) {
Here, you are firing off an asynchronous function (brands.find); when it is done, the anonymous function (function(err, doc){…}) will be called with the result.
> if(doc && doc.length) {
>
> console.log('Data doc: ' + doc);
>
> return res.render('brand_list', {
> shop: doc[0].shop.shop
> ,name: doc[0].
shop.name
> ,description: doc[0].shop.description
> ,products: doc
> ,pageTitle: doc[0].shop.shop
> ,pageUrl: '
http://backofficeserver.local/brand/' + identifier
> });
>
> } else {
>
> return res.render('listado_shop', {
> shop: doc[0].shop.shop
> ,name: doc[0].
shop.name
> ,description: doc[0].shop.description
> ,error: 'Not product´s found'
> ,pageTitle: doc[0].shop.shop
> ,pageUrl: '
http://backofficeserver.local/brand/' + identifier
> });
>
> }
Here you are rendering the jade template, using various values, including that global variable “identifier”. But between the time that this request started, and the time that the database returned the result and the template was rendered, another request may have started and overwritten the “identifier” variable with a different value.
Use “var” to declare your local variables so that they stay local and don’t pollute other requests.