error to save history chess into db sqlite django











up vote
0
down vote

favorite












I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js



winner = checkLogout !==  username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });


nodejsindex.js



var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});


gameviews.py



import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')


update code staticmain.js



socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});









share|improve this question
























  • Your except block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:, and then print("error saving game: %s" % ex)
    – John Gordon
    Nov 21 at 22:36










  • @JohnGordon, that notice: [21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7 but nodejs notice success the message was posted successfully
    – Tran Quoc Gia Cat
    Nov 21 at 22:43












  • That error message seems to say that msg_obj does not contain a gameId. Try printing msg_obj before the Game.object.create() call.
    – John Gordon
    Nov 21 at 22:50












  • @JohnGordon, gameId not here {'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId' and history not to convert to string
    – Tran Quoc Gia Cat
    Nov 21 at 22:57










  • Okay then, the error is obvious -- your caller is not supplying a gameId argument. How you handle that error is up to you.
    – John Gordon
    Nov 21 at 23:19















up vote
0
down vote

favorite












I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js



winner = checkLogout !==  username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });


nodejsindex.js



var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});


gameviews.py



import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')


update code staticmain.js



socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});









share|improve this question
























  • Your except block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:, and then print("error saving game: %s" % ex)
    – John Gordon
    Nov 21 at 22:36










  • @JohnGordon, that notice: [21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7 but nodejs notice success the message was posted successfully
    – Tran Quoc Gia Cat
    Nov 21 at 22:43












  • That error message seems to say that msg_obj does not contain a gameId. Try printing msg_obj before the Game.object.create() call.
    – John Gordon
    Nov 21 at 22:50












  • @JohnGordon, gameId not here {'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId' and history not to convert to string
    – Tran Quoc Gia Cat
    Nov 21 at 22:57










  • Okay then, the error is obvious -- your caller is not supplying a gameId argument. How you handle that error is up to you.
    – John Gordon
    Nov 21 at 23:19













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js



winner = checkLogout !==  username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });


nodejsindex.js



var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});


gameviews.py



import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')


update code staticmain.js



socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});









share|improve this question















I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js



winner = checkLogout !==  username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });


nodejsindex.js



var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});


gameviews.py



import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')


update code staticmain.js



socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});






python node.js django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 23:00

























asked Nov 21 at 22:32









Tran Quoc Gia Cat

406




406












  • Your except block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:, and then print("error saving game: %s" % ex)
    – John Gordon
    Nov 21 at 22:36










  • @JohnGordon, that notice: [21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7 but nodejs notice success the message was posted successfully
    – Tran Quoc Gia Cat
    Nov 21 at 22:43












  • That error message seems to say that msg_obj does not contain a gameId. Try printing msg_obj before the Game.object.create() call.
    – John Gordon
    Nov 21 at 22:50












  • @JohnGordon, gameId not here {'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId' and history not to convert to string
    – Tran Quoc Gia Cat
    Nov 21 at 22:57










  • Okay then, the error is obvious -- your caller is not supplying a gameId argument. How you handle that error is up to you.
    – John Gordon
    Nov 21 at 23:19


















  • Your except block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:, and then print("error saving game: %s" % ex)
    – John Gordon
    Nov 21 at 22:36










  • @JohnGordon, that notice: [21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7 but nodejs notice success the message was posted successfully
    – Tran Quoc Gia Cat
    Nov 21 at 22:43












  • That error message seems to say that msg_obj does not contain a gameId. Try printing msg_obj before the Game.object.create() call.
    – John Gordon
    Nov 21 at 22:50












  • @JohnGordon, gameId not here {'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId' and history not to convert to string
    – Tran Quoc Gia Cat
    Nov 21 at 22:57










  • Okay then, the error is obvious -- your caller is not supplying a gameId argument. How you handle that error is up to you.
    – John Gordon
    Nov 21 at 23:19
















Your except block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:, and then print("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36




Your except block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:, and then print("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36












@JohnGordon, that notice: [21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7 but nodejs notice success the message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43






@JohnGordon, that notice: [21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7 but nodejs notice success the message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43














That error message seems to say that msg_obj does not contain a gameId. Try printing msg_obj before the Game.object.create() call.
– John Gordon
Nov 21 at 22:50






That error message seems to say that msg_obj does not contain a gameId. Try printing msg_obj before the Game.object.create() call.
– John Gordon
Nov 21 at 22:50














@JohnGordon, gameId not here {'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId' and history not to convert to string
– Tran Quoc Gia Cat
Nov 21 at 22:57




@JohnGordon, gameId not here {'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId' and history not to convert to string
– Tran Quoc Gia Cat
Nov 21 at 22:57












Okay then, the error is obvious -- your caller is not supplying a gameId argument. How you handle that error is up to you.
– John Gordon
Nov 21 at 23:19




Okay then, the error is obvious -- your caller is not supplying a gameId argument. How you handle that error is up to you.
– John Gordon
Nov 21 at 23:19

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421369%2ferror-to-save-history-chess-into-db-sqlite-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421369%2ferror-to-save-history-chess-into-db-sqlite-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo