Cache variables?

248 views
Skip to first unread message

Anto

unread,
Jan 28, 2014, 11:43:05 AM1/28/14
to nod...@googlegroups.com
hello

I have a website developed in nodejs and use as jade template engine. I found a small bug and should be around me.

I have a dynamic URL parameter capturing a final and mongo used to search , always use the same scheme, same template but different data depending on the parameter.

In my application shows me all data from all parameters, as if the data were mixed, at first I thought it was my fault, that would be mixed in an intermediate variable and the application was debugger hours and did not find anything, the code was correct. I happened to restart the application and mysteriously works again (until new data is entered into the database), it's like will cache data and mingle among them. Does anyone know what can be and how can I fix it? Reset nodejs does not seem very professional and can not find how to fix it. Thank you. 

#Source

- Nodejs
app.get('/brand/:identifier', function(req, res){

    brands.find({ active: true, 'brandName' : { $regex : new RegExp(identifier, "i")} }).populate({
                                                                                                    path: 'shop'
                                                                                                    ,select: 'name shop description'
                                                                                                }).sort({dateAdded: 'desc'}).exec(function(err, doc) {
        if(doc && doc.length) {
                
            return res.render('brand_list', {
                                                shop: doc[0].shop.shop
                                                ,name: doc[0].shop.name
                                                ,description: doc[0].shop.description
                                                ,pageTitle: doc[0].shop.shop
                                                ,products: doc 
                   });
}
    
    });

});

- Jade (resumed)

each product in products
  li.product(id=product.identifier, name=product.identifier)
    a(href='/show/' + product.identifier, target='_blank')


Regards
Anto

Ryan Schmidt

unread,
Jan 28, 2014, 3:25:42 PM1/28/14
to nod...@googlegroups.com
Presumably you are storing some information in a variable that is not associated with the request, and is therefore shared among all requests. The code you posted doesn’t seem to have such a problem, so I’d look elsewhere in your code.

Anto

unread,
Jan 28, 2014, 8:39:49 PM1/28/14
to nod...@googlegroups.com
Hello !

I summarized part of the code to not dirty list with a lot of text.

I must indicate that I have programmed from scratch twice this part avoiding using variable names elsewhere. Console shows me correctly the data to be displayed on the web and often works until some time (I have not found exactly in which moment fails or pattern to use to replicate the fault) shows values of other schemes and no returns to display properly until you restart the application. I have not found a pattern, I tried to browse the remaining sections, insert data, delete, and always console shows me the correct data, but the browser displays other blended. Also in my development computer I think it works correctly (I do not work with real data), only fail in server, in which I run with forever+nodemon as I can not keep an open session.

I tried to let it run with screen + nodemon (I can not leave a session open ssh because if loses connection nodejs dies); moment works in a few hours able to tell them something (I deleted forever because it is the only thing different my computer). 

The code is as follows:

- Nodejs + Expressjs
                                                   
app.get('/brand/:identifier', function(req, res){

    identifier = req.params.identifier.toLowerCase();
    
    brands.find({ active: true, 'brandName' : { $regex : new RegExp(identifier, "i")} }).populate({
                                                                                                    path: 'shop'
                                                                                                    ,select: 'name shop description'
                                                                                                }).sort({dateAdded: 'desc'}).exec(function(err, doc) {
        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
                                                });
    
        }
    });

});

- Jade (resumed)

if error
  h4= error
else
  h4= 'List of products'
  if products
    each product in products
      li.product(id=product.identifier, name=product.identifier)
        a(href='/show/' + product.identifier, target='_blank')

They know of any tools to debug node js code? Thanks.

Regards
Anto


2014/1/28 Ryan Schmidt <googl...@ryandesign.com>
Presumably you are storing some information in a variable that is not associated with the request, and is therefore shared among all requests. The code you posted doesn't seem to have such a problem, so I'd look elsewhere in your code.

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/2SxmvchhHaM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Anto

unread,
Jan 28, 2014, 8:52:31 PM1/28/14
to nod...@googlegroups.com
It has failed again, no longer it can be ... I can not find the explanation:-S

Any idea? Thank you.

regards

Anto


2014/1/29 Anto <pot...@gmail.com>

Alex Kocharin

unread,
Jan 28, 2014, 9:01:58 PM1/28/14
to nod...@googlegroups.com
 
> identifier = req.params.identifier.toLowerCase();
 
If you missed "var" in this case, it would mean that you could have done the same thing elsewhere. It would easily be a cause for this.
 
Maybe dump "global" variable and look for non-standard entries there. Almost every one of them would be a bug waiting to show itself (although npm source code sometimes proves the opposite...).
 
 
29.01.2014, 05:40, "Anto" <pot...@gmail.com>:
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

Anto

unread,
Jan 28, 2014, 9:10:19 PM1/28/14
to nod...@googlegroups.com
Hello

Sorry, delete var to clean several console.log that I had to see the input parameters and other variables, to let them the cleanest code. These variables only the use in that part of the code, except the variable doc.

I'm desperate because I can not find the error. I'll do a dump of mongo and import it into my computer and work well with real data. Thanks

Regards
Anto


2014/1/29 Alex Kocharin <al...@kocharin.ru>

Ryan Schmidt

unread,
Jan 28, 2014, 10:55:56 PM1/28/14
to nod...@googlegroups.com

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.


Anto

unread,
Jan 29, 2014, 4:20:17 AM1/29/14
to nod...@googlegroups.com
Hello

I do not use global variables but if it had a bug, I changed the variables of this block for a name that is only used in this, I'll try it for a few hours and I will comment.

Anyway, for your assistance if someone is in the same situation I found "node-inspector" to debug nodejs code. thank you very much

Regards
Anto


2014/1/29 Anto <pot...@gmail.com>

Anto

unread,
Jan 29, 2014, 4:25:15 AM1/29/14
to nod...@googlegroups.com
hello Ryan 

If I use var to declare variables, was in the transcript of this message I delete it by mistake, by eliminating several console.log for you they had the cleanest code. I´m sorry.

Thanks !

Regards
Anto


2014/1/29 Ryan Schmidt <googl...@ryandesign.com>

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.

Anto

unread,
Jan 29, 2014, 4:39:57 AM1/29/14
to nod...@googlegroups.com
Hello

It is possible that even use var to define the variable, may exist a bug and take it as a global and have several requests resulting into failure?

I've also changed this part of the code being as follows:



app.get('/brand/:identifier', function(req, res){

    //var identifier = req.params.identifier.toLowerCase();
    var term = req.params.identifier.toLowerCase();
    
    brands.find({ active: true, 'brandName' : { $regex : new RegExp(term, "i")} }).populate({
                                                                                                    path: 'shop'
                                                                                                    ,select: 'name shop description'
                                                                                                }).sort({dateAdded: 'desc'}).exec(function(err, mongoProducts) {
        if(mongoProducts && mongoProducts.length) {
            
            console.log('Data doc: ' + mongoProducts);
            
            return res.render('brand_list', {
                                                shop: mongoProducts[0].shop.shop
                                                ,name: mongoProducts[0].shop.name
                                                ,description: mongoProducts[0].shop.description
                                                ,products: mongoProducts 
                                                ,pageTitle: mongoProducts[0].shop.shop
                                                ,pageUrl: 'http://backofficeserver.local/brand/' + term
                           });
                
        } else {
        
            return res.render('brand_list', {
                                                shop: mongoProducts[0].shop.shop
                                                ,name: mongoProducts[0].shop.name
                                                ,description: mongoProducts[0].shop.description
                                                ,error: 'Not product´s found'
                                                ,pageTitle: mongoProducts[0].shop.shop
                               ,pageUrl: 'http://backofficeserver.local/brand/' + term
                                                });
    
        }
    });

});

- Jade (resumed)

if error
  h4= error
else
  h4= 'List of products'
  if products
    each product in products
      li.product(id=product.identifier, name=product.identifier)
        a(href='/show/' + product.identifier, target='_blank')

I'll see if it works and the result I will comment. Thanks

Regards
Anto


2014/1/29 Anto <pot...@gmail.com>
hello Ryan 

Anto

unread,
Jan 29, 2014, 5:39:22 AM1/29/14
to nod...@googlegroups.com
Hello

It has failed again despite all this, the variables have the right data but once fault does not display properly. It's like jade take the cached data from another object ( -> products: mongoProducts). Thanks

Regards
Anto

Angel Java Lopez

unread,
Jan 29, 2014, 5:48:35 AM1/29/14
to nod...@googlegroups.com
Can you show:

- the retrieved document
- the jade ouput (relevant HTML from your browser source)

Can you add something "definitive" to Jade, template, like p= new Date() (I don't know if it is a valid Jade syntax). That is, a javascript expression returning the current date/time, not in the model, but in the view

In this way, you will be more info about the result. It's not clear (in my English understanding) if you have "cached" result (the same result that one hour or one minute, or one second ago), or "mangled" (the result is not what you expected but make senses, or the result is garbage, not expected, non sense).

Angel "Java" Lopez
@ajlopez







You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

Anto

unread,
Jan 29, 2014, 10:38:33 AM1/29/14
to nod...@googlegroups.com
Yourselves know her some javascript parser? Because I'm debugger and breadpoints jump on the rest of GET but not in this and I think there's some bad code above or something. And I see it in 6k lines ...

Thanks

Regards
Anto


2014-01-29 Angel Java Lopez <ajlop...@gmail.com>

Anto

unread,
Jan 29, 2014, 11:28:59 AM1/29/14
to nod...@googlegroups.com
Hi

node-inspector died and not on the browser. If you run hard and routines as planned, but I could not see the products object with failure.

Regars


2014-01-29 Anto <pot...@gmail.com>

Anto

unread,
Feb 3, 2014, 5:50:55 AM2/3/14
to nod...@googlegroups.com
Hi

Already solved.

This has been fixed by changing the name of the variable in Jade. Know if Jade uses local, global variables or is a bug. I understand that to be a template should be local but it seems that using global variables.

It's very strange, because if so, to enter another section, this variable would refresh and would show other values, but once caused the fault always showing the same data, a mixture of various data.

# Nodej

,listProducts: mongoProducts 

# Jade
each product in listProducts

Thanks !

Regards
Anto


2014-01-29 Anto <pot...@gmail.com>

Alex Kocharin

unread,
Feb 3, 2014, 2:16:10 PM2/3/14
to nod...@googlegroups.com
 
I'd really suggest to use jade with "self" option, this way you won't get confused where these variables are coming from.
 
 
03.02.2014, 14:51, "Anto" <pot...@gmail.com>:

Anto

unread,
Feb 12, 2014, 12:28:15 PM2/12/14
to nod...@googlegroups.com
Hello

Thanks for the trick. But my left me wondering if gets other variables (no other variable had that value, rather it was a mix of several values) because when entering other sections that refresh the variables did not alter the result, even if another wrong? Thanks.

Regards

Anto
Reply all
Reply to author
Forward
0 new messages