Learning Node.js
-
Awesome! When I come to a brick wall with my testing, I'll be sure to ask. Thanks!
-
First wall! Yeyy. Server passes data and when I try to use it in code it displays "object Object".
//Piece of server side code
io.on('connection', function(socket){
io.emit('chat message', { msg: 'testing' });
......//Piece of client side code
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
..... -
On the client side
msg
is a object that has a propertymsg
inside so trysocket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg.msg)); });
You should probably rename the property to text or something to prevent confusion.
-
Flawless Victory. Works well. I was under the impression that when passing data(key pair values), the receiving function needed the same variable name from the function it came from.
For example, the object name being sent from the server was msg. So I thought that the receiving function must be msg as well. Like this... socket.on('chat message', function(msg).....
But now I'm thinking that function(msg) can be named anything. Am I correct in this thinking? -
You can use whatever name you want on the client side. So the following is valid.
//server side io.emit('chat message', { text: 'testing' }); //clientside socket.on('chat message', function(whateverYouWant){ $('#messages').append($('<li>').text(whateverYouWant.text)); });
The important thing is the property name notice how the
text
on the server and client side match. -
I seriously want to give back to NodeBB contributing wise. I love NodeBB. When I learn enough code, I surely will!
-
I'm trying to create some html with a variable that can be adjusted.
<p>Bank Account:$</p> <p id="bankaccount">1000</p>
Not sure how I would do this. I want the value of 1000 to appear beside of Bank Account:$. However since both are in separate p tags, I can't figure it out. I would also like to save the value to a javascript variable.
EDIT* I figured to just create the entire string in Javascript. Easier to piece together.