Please need help to start working with NodeJS

@harrisonhjones
I have concern about where I should add the save file function. Do I need to add it inside the

spark.on('login', function() {

or I should add it after the

spark.on('login', function() {

and before the

spark.login({ username: 'Myemail@gmail.com', password: 'MyPassword'});

Thanks

I changed that as you mentioned in your reply, but I have this output:

New Event: {"data":"SA000FB00000000C00400400E","ttl":"1","published_at":"2015-06
-26T22:33:00.196Z","coreid":"00000000000000000","name":"MyEvent"}. Loggin
g it to file
Data: SA000FB00000000C00400400E
There was an error writing the data to the log file

I got some data in the text file, but not all of it. Also can I put a return after each data reading, so I can have the data line by line in the text file something like this:

SA00000000B00000000C00000000E
SA00000000B00000000C00000000E
SA00000000B00000000C00000000E

Thanks

Try this updated version and post the error output when it happens

1 Like

Thanks @harrisonhjones
Here is the output after the update:

New Event: {"data":"SA000FE000B00000000CC00400000E","ttl":"1","published_at":"20
15-06-26T22:48:28.425Z","coreid":"000000000000000000000","name":"MyEvent"}. L
ogging it to file
File log success

All the data still together in the same line in the text file :pensive:

Iā€™ll test this out at home tonight and update you when I figure out whatā€™s going on.

1 Like

@harrisonhjones Thanks a lot for your kindly help.
I appreciate your efforts.
Waiting for your reply tonight :wink:

Take a look over here for a possible solution to the new line issue: http://stackoverflow.com/questions/10384340/new-line-in-node-js

1 Like

Thanks a lot @Moors7
It worked for me by adding

\r\n instead of \n

Please I want to know what is the changing that should be done in the code to save the data SA00000000E in the text file instead of the whole line which showed in the console in case I want to show the time stamp with the data:

New Event: {"data":"SA000FB00000000C00400400E","ttl":"1","published_at":"2015-06
-26T22:33:00.196Z","coreid":"00000000000000000","name":"MyEvent"}. Loggin
g it to file
Data: SA000FB00000000C00400400E

Hello @harrisonhjones
I know that I'm bothering you, but please I want to know what is the changing that should be made in the code to save the data SA00000000E in the text file instead of the whole line which showed in the console in case I want to show the time stamp with the data:

New Event: {"data":"SA000FB00000000C00400400E","ttl":"1","published_at":"2015-06
-26T22:33:00.196Z","coreid":"00000000000000000","name":"MyEvent"}. Loggin
g it to file
Data: SA000FB00000000C00400400E

Sorry. Iā€™m confused. What would you like saved to the text file? What about just saving everything as a csv? So

{"data":"SA000FB00000000C00400400E","ttl":"1","published_at":"2015-06-26T22:33:00.196Z","coreid":"00000000000000000","name":"MyEvent"}

Would become

SA000FB00000000C00400400E,1,2015-06-26T22:33:00.196Z,00000000000000000,MyEvent
1 Like

Hello @harrisonhjones
Iā€™m sorry that I confused you. Now, the text file has the data, but I asked if I can include the time stamp with the data as you mentioned in your reply:

SA000FB00000000C00400400E,1,2015-06-26T22:33:00.196Z,00000000000000000,MyEvent

Therefore, if the data will have the time stamp in a csv format that will be great for me. If it is hard thing to do, then it will be enough to have the data only in the text file.
Thanks
Iā€™m sorry for bothering you again.

I would challenge you to try to figure it out yourself however, if you get stuck, it should look something like this:

fs.appendFile('myAwesomeLog.txt', data.data + "," + data.ttl + "," + data.published_at + "," + data.coreid + "," + data.name + "\r\n", function (err) {
1 Like

Thanks @harrisonhjones :grinning:
Actually, I expected to add the time stamp manually like this, but I thought that there may be a small modifying in the code to make it write whatever we see in the command line directly to the text fileā€¦
Iā€™ll try that when I return home.
Thanks a lot for your challengešŸ˜….

Dear @harrisonhjones
I tried adding the time stamp to the code, and it worked fine but the time is different from our us time. For example, the time now is 10:25 A.M. while in the console and the text file is shown as below:

SA001FC000B00000000C00400000E,2015-06-27T15:25:11.922Z,MyEvent

I don't know why there is about 5 hours between the actual time and the data's time while the time is correct in the dashboard which is 10:25 A.M.
Thanks.

Have a look at this, and then this :smile:

2 Likes

@Ahmedsa1983 I would personally keep it in GTM/UTC time (so itā€™s the same no matter where you deploy the code) but if youā€™d like it localized then have at it!

2 Likes

Thanks @Moors7
I read the two link, but I think this can be applied on the webpage more than applying it on the (.js) file right?
Thanks in advance.

Hello @harrisonhjones
I know that itā€™s the same, but the issue that my professor wanted to see the sensorā€™s actual time as in our city instead of the GTM/UTC time.
:relieved:
Thanks in advance.
Have a great day.

What webpage? It's just some javascript. That'll work in node.js just fine, since that's javascript as well. Just give it a try, and see what it does.

1 Like

Hello @Moors7
Do you mean I can try this code in the link below in my test.js?

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocalestring

Thanks.