NodeJS中的意外令牌'u' [英] Unexpected token 'u' in NodeJS

查看:129
本文介绍了NodeJS中的意外令牌'u'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个票务系统,在该系统中我收集了数组中的座位索引,但是当与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每当我只想收集depDatedesDate字段时.

我该如何解决?

解决方案

我猜是意外错误u",该错误日志中的u表示undefined

当您收集所有数据时,reqdata.selectedSeates不是undefined

但是仅收集depDatedesDate时,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 an Unexpected 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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆