问题描述
我正在制作一个票务系统,在该系统中我收集了数组中的座位索引,但是当与Node一起收集时,它被收集为我使用JSON.parse
解析的对象.
这是我的Angular Controller的控制器中的代码
$scope.bookThisSeat = function(seatIndex){
console.log(seatIndex);
$scope.ticket.selectedSeats.push(seatIndex);
console.log($scope.ticket)
}
$scope.registerTicket = function(){
$scope.ticket.selectedBus = $scope.ticket.selectedBus.id;
$scope.ticket.selectedSeats = angular.toJson($scope.ticket.selectedSeats);
console.log($scope.ticket);
$http.post('/tickets/create',$scope.ticket).
success(function(data){
if(data.success){
console.log("success")
}
}).
error(function(err){
console.log(err);
})
}
这是我的节点JS函数,用于收集输入数据
collect: function(req,res,next){
var fields = ['id','departure','destination','depDate','desDate','selectedSeats','amount','passenger_name','phone','selectedBus'];
var reqdata = {};
fields.forEach(function(entry){
if(typeof req.body[entry] !=='undefined')
reqdata[entry] = sanitize(req.body[entry]);
if(typeof req.query[entry]!== 'undefined')
reqdata[entry] = sanitize(req.query[entry]);
if(typeof req.params[entry] !== 'undefined')
reqdata[entry] = sanitize(req.params[entry]);
});
reqdata.selectedSeats = JSON.parse(reqdata.selectedSeats);
req.saf_tktdata = reqdata;
当我收集所有数据时,它工作正常,但返回一个Unexpected error u
每当我只想收集depDate
或desDate
字段时.
我该如何解决?
我猜是意外错误u",该错误日志中的u
表示undefined
当您收集所有数据时,reqdata.selectedSeates
不是undefined
但是仅收集depDate
或desDate
时,reqdata.selectedSeates
可能是undefinded
,而不能是JSON.parse
因此快速解决方法是
reqdata.selectedSeats = reqdata.selectedSeats ? JSON.parse(reqdata.selectedSeats) : null;
I am making a ticketing system where I collect the index of the seats in array but when collecting with Node it is collected as an Object which I parse using JSON.parse
.
Here's the code from the controller of my Angular Controller
$scope.bookThisSeat = function(seatIndex){
console.log(seatIndex);
$scope.ticket.selectedSeats.push(seatIndex);
console.log($scope.ticket)
}
$scope.registerTicket = function(){
$scope.ticket.selectedBus = $scope.ticket.selectedBus.id;
$scope.ticket.selectedSeats = angular.toJson($scope.ticket.selectedSeats);
console.log($scope.ticket);
$http.post('/tickets/create',$scope.ticket).
success(function(data){
if(data.success){
console.log("success")
}
}).
error(function(err){
console.log(err);
})
}
And here's my node JS function to collect the input data
collect: function(req,res,next){
var fields = ['id','departure','destination','depDate','desDate','selectedSeats','amount','passenger_name','phone','selectedBus'];
var reqdata = {};
fields.forEach(function(entry){
if(typeof req.body[entry] !=='undefined')
reqdata[entry] = sanitize(req.body[entry]);
if(typeof req.query[entry]!== 'undefined')
reqdata[entry] = sanitize(req.query[entry]);
if(typeof req.params[entry] !== 'undefined')
reqdata[entry] = sanitize(req.params[entry]);
});
reqdata.selectedSeats = JSON.parse(reqdata.selectedSeats);
req.saf_tktdata = reqdata;
It works fine when I am collecting all the data, but it returns anUnexpected error u
whenever I want to only collect the depDate
or desDate
fields.
How can I solve this?
I guess "Unexpected error u", the u
in that error log means undefined
when you collect all the data, reqdata.selectedSeates
is not undefined
but when you collect depDate
or desDate
only, probably reqdata.selectedSeates
is undefinded
and it cannot be JSON.parse
so a quick fix is
reqdata.selectedSeats = reqdata.selectedSeats ? JSON.parse(reqdata.selectedSeats) : null;
这篇关于NodeJS中的意外令牌'u'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!