0

index.js

var outputFilename = 'C:/askhere/public/javascripts/data.json';
                    fs.writeFile( outputFilename, JSON.stringify(json_obj), "utf8", function(err) {
                        if(err) {
                            console.log(err);
                        } else {
                            console.log("Jobs Data updated in " + outputFilename + " file.");
                        }
                    });
res.render('graph',{uname:req.session.userName,gdata:JSON.stringify(json_obj)});

graph.ejs

                    <script type="text/javascript" src="/javascripts/graph.js">

                    </script>

graph.js

            /*var data = [{"label":"1990", "value":16},
    {"label":"1991", "value":56},
    {"label":"1995", "value":16},
];    var data;
    d3.json("data.json", function(error, json) {
        if (error) {return console.log(error);}
        else {data = json; console.log("data updated in graph.js");}
    });

Now this code getting "data" update from server in graph.js file but i cant see result in graph.ejs file

hammad038
  • 13
  • 1
  • 6

1 Answers1

0

You can use

 d3.json('http://domain.com/data.json', function(data) {
     // actual logic...
});

to access data served as json from the server.

Otherwise you can also add an inline script:

<script type="text/javascript">
     var data = <%=gdata%>;
</script>

Side note : you can't have inline script with src attributes

<script type="text/javascript" src="/javascripts/graph.js">
    var data = <%=gdata%>; // This code will be discarded
</script>

Also, i suggest that you read this question, which may actually be your underlying problem.

Community
  • 1
  • 1
Damien Fayol
  • 958
  • 7
  • 17
  • /*var data = [{"label":"1990", "value":16}, {"label":"1995", "value":16}, ];*/ var data; d3.json("data.json", function(error, json) { if (error) {return console.log(error);} else {data = json; console.log("data updated in graph.js");} }); data.json file is updated but there are no results Graph : <%=gdata%> – hammad038 Feb 13 '16 at 13:38
  • @Shah038 what do you mean by "there are no results" ? – Damien Fayol Feb 13 '16 at 13:44
  • i cant see the result of in graph.ejs , but when i use old data variable i can see result graph. – hammad038 Feb 13 '16 at 13:56
  • Can you update your question with the actual d3.js code ? the one creating the graph – Damien Fayol Feb 13 '16 at 14:02
  • Your problem is that you can't just assign `data = json`, you have to keep all your code inside the `function(error, json) {}` callback – Damien Fayol Feb 14 '16 at 16:01