if have the following code:
socket.on('getNeighbors', function(data)){
if( typeof data !== undefined ){
do something...
}
});
But when I try to send this packet without data my server crashes with the TypeError: Cannot read property '...' of undefined (in this case data).
I understand the reason behind the error, but how can I prevent my server from crashing? How can I catch the error before I get inside of the if-clause? I thought I can manage this by checking if data is undefined, but that is not working :|
Greetings
Related
I am getting an error that says SyntaxError: Unexpected token a in JSON at position 0
and I cannot find any information on what "a" means. I know that the JSON is not undefined.
Can anyone help me understand what is causing this error?
Here is the code block that is causing the error:
let db_creds = await getDBCredentials();
console.log(db_creds)
const pool = new Pool(JSON.parse(db_creds['SecretString']));
console.log(pool)
Unexpected Token < in JSON at Position 0. From time to time when working with JSON data, you might stumble into errors regarding JSON formatting. For instance, if you try to parse a malformed JSON with the JSON. ... json() method on the fetch object, it can result in a JavaScript exception being thrown.
What Is JSON and How to Handle an “Unexpected Token” Error
Well for me it was because the port was already in use and it must be sending HTML, you can try killing the port by running the below command in cmd(admin mode)
taskkill /F /IM node.exe
I have this:
<script>
function gettingJSON(){
$.getJSON("http://api.openweathermap.org/data/2.5/weather?q=London&appid=",function(json){
console.log(JSON.stringify(json));
});
}
</script>
when i run this, and look at my console, i get this erorr:
[Error] Failed to load resource: the server responded with a status of 401 (Unauthorized) (weather, line 0)
my api key matches the one in the openweathermap dashboard, so wondering y its not acc working. Maybe i'm missing something. any ideaS?
I think you just need to put your api key in the query string.
http://api.openweathermap.org/data/2.5/weather?q=London&appid=[HERE]
Moreover it requires a couple of hours to be activated and ready to use.
Hello, some time ago, i started to make a userscript (tampermonkey script) where i get the active websocket, i have tried to get the websocket when the websocket send a message:
let ws;
window.WebSocket.prototype.send = function(){
if(!ws) {
ws = this
console.log(ws)
}
}
but not working, so i tried to handle messages but nothing showing up when a message was received:
let ws = window.WebSocket;
ws.onmessage = function(msg){
console.log(msg.data)
}
Finally i choosed to get websocket binaryType for reproduce it, but when i send
window.WebSocket.prototype.binaryType
in devtools, i got this error
(in latest Firefox)
TypeError: 'get binaryType' called on an object that does not implement interface WebSocket.
(in latest Microsoft Edge)
TypeError: Illegal Invocation
(i'm not good with English sorry, i'm French)
Thanks for answers
Thanks for answer, i have edited my script, code here:
let ws;
unsafeWindow.WebSocket.prototype.oldSend = unsafeWindow.WebSocket.prototype.send
unsafeWindow.WebSocket.prototype.send = function(m) {
if(!ws) {
ws = this
alert(ws)
}
unsafeWindow.WebSocket.prototype.oldSend.apply(this, [m])
}
//need that for get the websocket
unsafeWindow.WebSocket.prototype.send("ping")
This is working, i get the websocket but, i got an error:
TypeError: Illegal Invocation at WebSocket.unsafeWindow.WebSocket.prototype.send
what should i do?
[UPDATED!]
Line Error:
unsafeWindow.WebSocket.prototype.oldSend.apply(this, [m])
was called on the WebSocket object, not in the window.
Juste need to replace .apply to .bind
Corrected Line:
unsafeWindow.WebSocket.prototype.oldSend.bind(unsafeWindow, m)
So, I have a node server, running expressjs io (uses socket.io), and I'm building a grid map that tracks coordinates in a database.
Only, I've run into a peculiar issue in that my sockets only listen sometimes.
At first there was no error message, and only by chance I let the page run and I got this error.
Uncaught TypeError: Cannot call method '0' of undefined UkPS99A_w96Ae0K570Nt?t=1395276358213&i=0:1
When I click on the file UkPS99A_w96Ae0K570Nt?t=1395276358213&i=0:1 I get this code:
io.j[0]("8::");
If I refresh the page, every few times it will suddenly work find for about 10 tile clicks, and then it stops working. My database is updating properly until the sockets basically die out.
Here is where I send the coordinates in my map:
io.emit("move", {x:this.x,y:this.y});
Server listening:
app.io.route('move', function(req) {
con.getConnection(function(err){
if (err) console.log("Get Connection Error.. "+err);
//removed query because redundant
req.io.emit("talk", {x:req.data.x,y:req.data.y});
});
});
and my socket script:
io.on("talk",function(data) {
console.log(data.x,data.y);
});
My script includes are at the bottom of the page in this order:
<script src="socket.io/socket.io.js"></script>
<script>io = io.connect();</script> <!-- open the socket so the other scripts can use it -->
<script src="../js/sock.js"></script>
<script src="../js/map.js"></script>
Is there something I'm doing wrong to that the socket seems to lose connection and throw some sort of error?
Update: I left the server running longer and a couple more error messages popped up in my console:
Uncaught TypeError: Cannot call method 'close' of null socket.io.js:1967
Uncaught TypeError: Cannot call method 'close' of null socket.io.js:1967
Uncaught TypeError: Cannot call method 'onClose' of null
More update: altered the connection line and added the proper CORS to my server.js
io = io.connect('http://sourceundead.com', {resource : 'socket.io'});
Still the same issue.
You seem to have a connection attrition as you never release them to the pool.
Assuming con is the (bad) name of your pool, instead of
app.io.route('move', function(req) {
con.getConnection(function(err){
if (err) console.log("Get Connection Error.. "+err);
//removed query because redundant
req.io.emit("talk", {x:req.data.x,y:req.data.y});
});
});
you should have something like
app.io.route('move', function(req) {
con.getConnection(function(err, connection){
if (err) console.log("Get Connection Error.. "+err);
//removed query because redundant
req.io.emit("talk", {x:req.data.x,y:req.data.y});
connection.release();
});
});
Be careful that using connections must be done with care to ensure they're always released, and it's a little tedious to do especially when handling errors as soon as you have a few queries to do when doing a task.
At some point you might want to use promises to make that easier. Here's a blog post about using bound promises to ease database querying in node.js.
I am trying to use acknowledgements with socket.io as defined in http://socket.io/#how-to-use "Sending and getting data (acknowledgements)".
My client code looks like
sio.emit('ferret', 'tobi', function (data) {
console.log(data);
});
And my server code is:
socket.on('ferret', function (name, fn) {
fn('woot');
});
But I'm getting an error that's crashing my node server:
fn('woot');
23:13:48 web.1 | ^
23:13:48 web.1 | TypeError: undefined is not a function
Which I find strange because if I add:
console.log(name);
console.log(fn);
, name will log Tobi, but for what ever reason fn is logging undefined.
Any information would be awesome!!
In case anyone was having this problem, make sure that on the client, the emit has an acknowledgement function as the last parameter. If you write the server side code and restart the server again, it will give an error unless you have the client side page refreshed with the new code. Although I had written the code on both client and server, I kept getting the error on my server because I had not refreshed my client side page.