阿鸷 2022-05-30 23:50 采纳率: 100%
浏览 119
已结题

Node.js 报错 SyntaxError: Unexpected identifier at new Function (可能与res.render有关)

Node.js 报错 SyntaxError: Unexpected identifier at new Function (可能与res.render有关)

 /**
     * 实现登录验证功能
     */
    app.post('/login',function (req,res) {
        var  name=req.body.username.trim();
        var pwd=req.body.pwd.trim();
    console.log('username:'+name+'password:'+pwd);
        var selectMovieInfoSQL="select movieid,moviename,picture from movieinfo limit 1000";
        var movieinfolist=[];
    connection.query(selectMovieInfoSQL,function(err,rows,fields){
       if (err) throw  err;
       //console.log('movieids length is:'+rows.length);
       //console.log('movieid is:'+rows[0].movieid);
           //console.log('moviename is:'+rows[0].moviename);
           movieinfolist=rows;
           });
        
        var selectSQL = "select * from user where username = '"+name+"' and password = '"+pwd+"'";
        connection.query(selectSQL,function (err,rows,fields) {
            if (err) throw  err;
            function randomFrom(lowerValue,upperValue)
{
    return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
}

            //console.log('userid is:'+rows[0].userid);

            var lowerValue=0;
            var upperValue=movieinfolist.length;
            var index=randomFrom(lowerValue,upperValue);
            //console.log('movieid random is:'+movieinfolist[index].movieid);
            //console.log('moviename random is:'+movieinfolist[index].moviename);
            var movielist=[];
            var movieNumbers=10;
            for (var i=0;i<movieNumbers;i++){
              index=randomFrom(lowerValue,upperValue);
              movielist.push({movieid:movieinfolist[index].movieid,moviename:movieinfolist[index].moviename,picture:movieinfolist[index].picture});
              
}

            //for(var item in movielist){
            //  console.log('item is:'+item);
            //  console.log('movieid is:'+movielist[item].movieid);
            //  console.log('moviename is:'+movielist[item].moviename);
            //}      
            res.render('personalratings',{title:'Welcome User',userid:rows[0].userid,username:rows[0].username,movieforpage:movielist});
    });
});
运行结果及报错内容

img


(打码的是个人信息)

我的解答思路和尝试过的方法

上网找了很久,都没办法解决

我想要达到的结果

代码可以运行

完整代码如下:



/**
 * express接收html传递的参数
 */

var express = require('express');
var bodyParser = require('body-parser')
const spawnSync = require('child_process').spawnSync;
var app = express();
var mysql = require('mysql');
var http = require("http");
app.set('view engine', 'jade');
app.set('views', './views');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())


/**
 * 配置MySQL
 */
var connection = mysql.createConnection({
  host: '127.0.0.1',
  user: 'root',
  password: 'root',
  database: 'movierecommend',
  port: '3306'
});
connection.connect();

/**
 * 跳转到网站首页
 */
app.get('/', function (req, res) {
  res.render('index', { title: '首页' });
})

/**
 * 跳转到登录页面
 */

app.get('/loginpage', function (req, res) {
  res.render('loginpage', { title: '登录' });
})


/**
 * 实现登录验证功能
 */
app.post('/login', function (req, res) {
  var name = req.body.username.trim();
  var pwd = req.body.pwd.trim();
  console.log('username:' + name + 'password:' + pwd);
  var selectMovieInfoSQL = "select movieid,moviename,picture from movieinfo limit 1000";
  var movieinfolist = [];
  connection.query(selectMovieInfoSQL, function (err, rows, fields) {
    if (err) throw err;
    //console.log('movieids length is:'+rows.length);
    //console.log('movieid is:'+rows[0].movieid);
    //console.log('moviename is:'+rows[0].moviename);
    movieinfolist = rows;
  });

  var selectSQL = "select * from user where username = '" + name + "' and password = '" + pwd + "'";
  connection.query(selectSQL, function (err, rows, fields) {
    if (err) throw err;
    function randomFrom (lowerValue, upperValue){
      return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
    }

    //console.log('userid is:'+rows[0].userid);

    var lowerValue = 0;
    var upperValue = movieinfolist.length;
    var index = randomFrom(lowerValue, upperValue);
    //console.log('movieid random is:'+movieinfolist[index].movieid);
    //console.log('moviename random is:'+movieinfolist[index].moviename);
    var movielist = [];
    var movieNumbers = 10;
    for (var i = 0; i < movieNumbers; i++) {
      index = randomFrom(lowerValue, upperValue);
      movielist.push({ movieid: movieinfolist[index].movieid, moviename: movieinfolist[index].moviename, picture: movieinfolist[index].picture });

    }

    //for(var item in movielist){
    //  console.log('item is:'+item);
    //  console.log('movieid is:'+movielist[item].movieid);
    //  console.log('moviename is:'+movielist[item].moviename);
    //}      
    res.render('personalratings', { title: 'Welcome User', userid: rows[0].userid, username: rows[0].username, movieforpage: movielist });
  });
});

/**
 * 跳转到注册页面
 */

app.get('/registerpage', function (req, res) {
  res.render('registerpage', { title: '注册' });
})

/**
 * 实现注册功能
 */
app.post('/register', function (req, res) {
  var name = req.body.username.trim();
  var pwd = req.body.pwd.trim();
  var user = { username: name, password: pwd };
  connection.query('insert into user set ?', user, function (err, rs) {
    if (err) throw err;
    console.log('register success');
    res.render('registersuccess', { title: '注册成功', message: name });
  })
})
/**
 * 把用户评分写入数据库
 */

app.post('/submituserscore', function (req, res) {
  var userid = req.body.userid;
  var moviescores = [];
  var movieids = [];
  req.body.moviescore.forEach(function (score) {
    //console.log('the score is:'+score);
    moviescores.push({ moviescore: score });
  });
  req.body.movieid.forEach(function (id) {
    //console.log('the id is:'+id);
    movieids.push({ movieid: id });
  });

  //for(var item in movieids){
  //   console.log('item is:'+item);
  //   console.log('movieid is:'+movieids[item].movieid);
  //}
  //for(var item in moviescores){
  //   console.log('item is:'+item);
  //   console.log('moviescore is:'+moviescores[item].moviescore);
  //}
  //删除该用户历史评分数据,为写入本次最新评分数据做准备
  connection.query('delete from  personalratings where userid=' + userid, function (err, result) {
    if (err) throw err;
    console.log('deleted');
    //console.log(result);
    //console.log('\n');
  });
  //生成评分时间戳
  var mytimestamp = new Date().getTime().toString().slice(1, 10);
  //console.log('mytimestamp2 is:'+mytimestamp);
  for (var item in movieids) {
    //把每条评分记录(userid,movieid,rating,timestamp)插入数据库  
    var personalratings = { userid: userid, movieid: movieids[item].movieid, rating: moviescores[item].moviescore, timestamp: mytimestamp };
    connection.query('insert into personalratings set ?', personalratings, function (err, rs) {
      if (err) throw err;
      console.log('insert into personalrating success');
    });
  }
  var selectUserIdNameSQL = 'select userid,username from user where userid=' + userid;
  connection.query(selectUserIdNameSQL, function (err, rows, fields) {
    if (err) throw err;
    res.render('userscoresuccess', { title: 'Personal Rating Success', user: rows[0] });
  });

});

/**
 * 调用Spark程序为用户推荐电影并把推荐结果写入数据库,把推荐结果显示到网页
 */
app.get('/recommendmovieforuser', function (req, res) {
  //console.log('result point 1');
  const userid = req.query.userid;
  const username = req.query.username;
  //console.log('recommendation userid is:'+userid);      
  const path = '/input_spark';
  //调用Spark程序为用户推荐电影并把推荐结果写入数据库
  let spark_submit = spawnSync('/usr/local/spark/bin/spark-submit', ['--class', 'recommend.MovieLensALS', ' /home/litong/Desktop/Film_Recommend/out/artifacts/Film_Recommend_jar/Film_Recommend.jar', path, userid], { shell: true, encoding: 'utf8' });
  //console.log('spark running result is:'+spark_submit.stdout);
  //从数据库中读取推荐结果,把推荐结果显示到网页
  var selectRecommendResultSQL = "select recommendresult.userid,recommendresult.movieid,recommendresult.rating,recommendresult.moviename,movieinfo.picture from recommendresult inner join movieinfo on recommendresult.movieid=movieinfo.movieid where recommendresult.userid=" + userid;
  var movieinfolist = [];
  connection.query(selectRecommendResultSQL, function (err, rows, fields) {
    if (err) throw err;
    //console.log('result point 3');
    //console.log('movieids length is:'+rows.length);
    //console.log('movieid is:'+rows[0].movieid);
    //console.log('moviename is:'+rows[0].moviename);
    console.log('read recommend result from database');

    for (var i = 0; i < rows.length; i++) {
      console.log('forxunhuan:i=' + i);
      movieinfolist.push({ userid: rows[i].userid, movieid: rows[i].movieid, rating: rows[i].rating, moviename: rows[i].moviename, picture: rows[i].picture });
    }

    //for(var item in movieinfolist){
    //   console.log('result point 6');
    //   console.log('item is:'+item);
    //   console.log('userid is:'+movieinfolist[item].userid);
    //   console.log('movieid is:'+movieinfolist[item].movieid);
    //   console.log('moviename is:'+movieinfolist[item].moviename);
    //   console.log('rating is:'+movieinfolist[item].rating);
    //   console.log('picture is:'+movieinfolist[item].picture);
    //}

    res.render('recommendresult', { title: 'Recommend Result', message: 'this is recommend for you', username: username, movieinfo: movieinfolist })
  });

})

var server = app.listen(3000, function () {
  console.log("movieroad server start......");
})

  • 写回答

4条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 6月9日
      • 已采纳回答 6月1日
      • 修改了问题 5月31日
      • 修改了问题 5月30日
      • 展开全部

      悬赏问题

      • ¥15 VS2019不能设置QT的verson了,设置了点确定又消失了,怎么解决呢?
      • ¥15 用java解决,要解释一下给我
      • ¥100 项目经理想学开发牛牛指点迷津有偿
      • ¥15 vscode 配置时出现问题
      • ¥15 pygame安装失败
      • ¥15 数据库原理怎么从文字描述中的句子推出(分析)函数依赖?
      • ¥15 想要使用有序logistic模型,因变量收集结果为以下,可以继续分析吗?
      • ¥15 C语言的问题 求尽快解答一下
      • ¥15 sql相关子查询的一道选择题
      • ¥15 关于图像检测的问题#