last day (14 days later) » 

7:23 AM
0
Q: How can I take variable from ejs to app.js in Node.js?

paulc1111I'm pretty newbie in node.js and trying to pass JSON data from ejs to app.js but I don't know how to pass through from ejs to app.js. What I did was var stringify1 = <%- JSON.stringify(jsonfile_1) %>; from in ejs, and then after the user clicks save button, let it goes to '/save' Then I used ...

 
Can you provide more code, it's seems to be incomplete and not exactly clear what you are trying to achieve. I believe the saveText(JSON.stringify(jsonfile_1.channel), "hello.json"); makes an Ajax call so you can grab the params like req.query.param_name
 
@SarojSasmal wait let me EDIT
@SarojSasmal I edited it.. Please help me out here.... :'(
@SarojSasmal I think your very first comment is the right way.. Can you give me more specifically with the code?? :'(
 
@paulc1111 hang on .lets get to the bottom of this .
 
@SarojSasmal Thank you so much for helping me out, I actually made hidden div and put json data to the div value, How can I call from server side? I tried req.body.jsonjson but it doesn't work. My hidden div is like this <div id="jsonjson" name="json1" value=""></div>
 
@paulc1111 It should be req.body.json1 if you are posting a form or req.query.json for Ajax GET. You have to access it by it's name not ID
 
7:23 AM
@SarojSasmal Hm.. but it gives me undefined from console.log..
 
Hi Let's chat over here
Just want to ask how to send the hidden Json to server.. I mean do you use Ajax or it's a form that's get submitted.
@paulc11111 I believe <li><a onclick= "save()" id="imageSave" href="/save">Save</a></li> this sends the request to /save route..
 
hi,
 
This is a simple link i think no param is sent to server as it's not a form.
 
oh man I think because of what you replied at the last...
because of <li><a onclick= "save()" id="imageSave" href="/save">Save</a></li> this sends the request to /save route..
and I built code like app.get('/save',...)
this is why I'm not getting req.body.json1
Isn't it?
 
Correct
 
7:32 AM
hm then should I write like app.post('/save'?
but it will not get save though.. hm..
 
I think you should wrap the button and the hidden field inside a form and when the submit button is hit, You can get it by req.body.json1
 
What should I do... do you have any suggestions....? :'(
 
Hang on
 
omg.. Thank you so much for helping me out..
 
hang on it might take bit time.. i am preparing a form for you..
 
7:33 AM
is that like hidden form?
them should I delete hidden div that I made?
then*
oh ok
sorry I'm not pressuring or something, just want to learn and talk..:)
 
Just one question <li><a onclick= "save()" id="imageSave" href="/save">Save</a></li> is this a sidebar header link.? Is it fine i put inside a form ?
 
it's like a header link
 
Okay
 
<header>
  <nav class="navbar navbar-inverse">
    <div class="collapse navbar-collapse" id="navbar-collapse">
        <ul class="nav navbar-nav navbar-right">
          <li><a onclick = "show()" id="imageLoad">Upload</a></li>
          <li><a onclick= "save()" id="imageSave" type="submit">Save</a></li>
          <li><a onclick= "save()" id="imageSave" type="submit">Back</a></li>
        </ul>
      </div>
    </nav>
  </header>
like this
 
No prob give me a minute
 
7:40 AM
k thanks
 
<form action="/save" id="hdnJsonForm">
<input type="hidden" name="json1" id="jsonjson">
</form>

In javascript you have to submit the form when the save link is clicked. You can acheive this by
$(document).on('click','#imageSave',function(){
$('#hdnJsonForm').submit();
});
But prior to that you have to set the hidden form value.
Let me know if it works or not..
 
ok thanks wait a min
 
Did you get the idea ? When we click the ImageSave link it submits the form and the Post route will be called. app.post('/save', isLoggedIn, function(req, res){
console.log(stringify1);
});
 
ohh yeah.....
not the GET it is POST
and it will not be stringify right?
should be req.body.json1
right?
 
Yes
You can stringify it on server side as well
 
7:45 AM
and I have to write like below
document.getElementById("jsonjson").value = JSON.stringify(jsonfile_1.channel);
correct?
in save function
 
Yeah
 
Also, that <form></form> should not be in header too right
?
 
No need
You can put it inside body
 
yeah I asked just in case :)
wait I'm running
 
Sure
We will fix this up Buddy
 
7:48 AM
it gives me Cannot GET /save?json1=%7B%22B8%3A27%3AEB%3A56%3AF9%3A7F%22%3A%5B%22343%22%2C%22642%22%5D%2‌​C%22
 
Sorry Set method="POST" inside the form. I forgot to mention.
 
like this?
<form action="/save" id="hdnJsonForm" method="POST">
correct...?
 
oh wow
 
Did it work?
 
7:50 AM
yupyup
lol
 
Ohooo...
 
Thanks a lot bro..
 
Welcome
 
sorry for taking your time..
 
Glad it help. Someone helped me when i was needed..
 
7:51 AM
damn you are genius!
:D
oh man I really appreciated..
 
Thanks
 
I spent like 3~4hours for only this//
 
One more thing
 
yes sir
 
You can achieve the same thing with app.get route but you need to pass the json as query param. But post is safe as the params go over body .
 
7:54 AM
ah ha.. I see...
I have to study alot more
 
I will add an answer please upvote it. Hope you don't mind
 
of course
oh
also Can I ask
simple question?
 
Yah
Sure
 
I'm trying to write that json data to file and save it
should I use filestream to do it?
such as writestream or readstream
 
Yes i that would do it. But i think there should be some npm library to do this. Give me a minute
 
7:58 AM
holy cow.. you are my life savior... T_T
 
If you have skypeid or something we can hangout if you have no prob, Just send me a request in case you need help.. mfsi_saroj.sasamal
 
oh.. I'm not available to do skype right now...
sorry...
 
No prob
9
Q: write/add data in JSON file using node.js

IsoftmasterI am trying to write JSON file using node from loop data eg var jsonfile = require('jsonfile'); for (i=0; i <11 ; i++){ jsonfile.writeFile('loop.json', "id :" + i + " square :" + i*i); } outPut in loop.json is id :1 square : 1 but I want output file like this (below) and also if I run th...

 
I think I can do it.. :) Thanks for the help Saroj Sasmai.. and sorry for taking your bunch of time
 
No prob. Happy coding.. :)
 
8:00 AM
didn't mean to but you are so nice and genius!
Happy Coding! :)
 
Thanks.
 
I'll ask if I'm stuck! :) lol
 
Sure
 

last day (14 days later) »