0

I try every possible solution that is available in SO. like using global or using bind but can not access the global variable. here is my code i am trying to generate sitemap in nodejs here is my .js file.

    module.exports = function (portal) {
    var mongoose = require('mongoose');
    var News = mongoose.model('News');
    var urls ="";
    var gensitemap =  function(){
    urls = "abc";

    console.log(urls);// print abc as expected
    News.find().exec(function (err, news) {
      news.forEach(function (t) {
        urls += "<url><loc>"+cdn_url+"/news"+t.slug+"</loc>"+"</url>";
       });
    });
    console.log(urls);// print still abc as not  expected it should be change
    }   
    var allUsers = portal.setupContext({});
    app.get('/gensitemap',allUsers ,gensitemap);
  };

here you can see i use url variable befor and after News.find() method.but instead of change the url its is printing same output.

Any idea i already try global or async.parallel({}) but same issue in News.find() node js treat that variable as different.

Any help ?

UPDATE :

After reading the comments and some of the mention question i try this

    Events.find().exec(function (err1, events) {
            events.forEach(function (t1) {
                url += "<url><loc>"+cdn_url+"/event"+t1.slug+"</loc>"+changefreq+priority+"</url>";
            });
            News.find().exec(function (err2, news) {
                    news.forEach(function (t2) {
                        url += "<url><loc>"+cdn_url+"/news"+t2.slug+"</loc>"+changefreq+priority+"</url>";
                    });
                    var static_urls = ['about-us','categories','overview','media','contact-us','login','register','pages/en/faq'];
                            for(var i = 0 ;i < static_urls.length;i++){
                                url += "<url><loc>"+"http://192.168.10.38:3000/"+static_urls[i]+"</loc>"+"<changefreq>weekly</changefreq>"+"<priority>1</priority>"+"</url>";
                            }
                    console.log(url);

//this url(variable) print only the changes i made in above for loop.. :-(
                });
        });

I also notice after waiting some time i am able to see the change but they are not in same order.

13
  • 3
    Possible duplicate of How to return the response from an asynchronous call? Commented Nov 20, 2017 at 11:24
  • try to this link and slove qution; stackoverflow.com/questions/10987444/… Commented Nov 20, 2017 at 11:24
  • but my variable are in the same file @DharmikUnagar Commented Nov 20, 2017 at 11:26
  • 3
    Yes it is. The answers to the question solve exactly your problem. But keep trying to do something with global variables if you know it better... :-) Commented Nov 20, 2017 at 11:28
  • 1
    Your problem is you don't understand the life cycle of an asynchronous call in NodeJS. You don't need to use global or async (using global is an 95% bad idea in all cases). Your just need to understanding is useless to try too access urls before mongo reply to your request. Commented Nov 20, 2017 at 13:09

1 Answer 1

1
    module.exports = function (portal) {
    var mongoose = require('mongoose');
    var News = mongoose.model('News');
    var urls ="";
    var gensitemap =  function(){
    urls = "abc";

    console.log(urls);// print abc as expected
    News.find().exec(function (err, news) {//this block of code gets executed after the News.find().exec() function return . Anything you want to do after the exec function call get a response you have to do it here. inside this call back function.

      news.forEach(function (t) {
        urls += "<url><loc>"+cdn_url+"/news"+t.slug+"</loc>"+"</url>";
       });
      //if you log the url here you should find ur desired value
      console.log(urls);
    });
    //This is getting called before your News.find().exec() function returns any value.
    console.log(urls);// print still abc as not  expected it should be change
    }   
    var allUsers = portal.setupContext({});
    app.get('/gensitemap',allUsers ,gensitemap);
  };
Sign up to request clarification or add additional context in comments.

3 Comments

Look i agree in the News.find() call back i get my desire out put but i have more 3 other call like News.find() then what i suppose to do ?
depends on how you want to use them. You can look at these packages. npmjs.com/package/async or bluebirdjs.com/docs/getting-started.html
Or the classic ES6 Promise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.