error to save history chess into db sqlite django












0














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 '18 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 '18 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 '18 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 '18 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 '18 at 23:19
















0














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 '18 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 '18 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 '18 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 '18 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 '18 at 23:19














0












0








0







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 '18 at 23:00







Tran Quoc Gia Cat

















asked Nov 21 '18 at 22:32









Tran Quoc Gia CatTran 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 23:19












0






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',
autoActivateHeartbeat: false,
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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python