mysql节点:发送后不能设置标头

时间:2018-03-04 11:47:45

标签: mysql node.js express

我正在尝试获取目录中的电影列表,解析标题,获取TMDB上的电影信息,而不是检查电影信息是否存储在mysql数据库中,如果未存储,则将信息插入数据库。

我正在使用NodeJS/Expressmysql

到目前为止,这是我的代码:

exports.checkForMovies = function (req, res, next) {
  const testFolder = './test/';
  var movieList = [];
  var movieResultsPromise = [];
  var movieResults = [];

  fs.readdirSync(testFolder).forEach(file => {
    movieList.push(tnp(file));
  });

  movieList.forEach(movie => {
    var waitPromise = searchTMDB(movie.title);
    movieResultsPromise.push(waitPromise);
  });

  Promise.all(movieResultsPromise).then(result => {
    movieResults = result;

    movieResults.forEach(movie => {
      checkMoviesInDB(movie.id, (err, data) => {
        if (err) {
          console.log(err)
        }

        if (data && data.update === true) {

          var movieObj = {
            m_tmdb_id: movie.id
          };

          insertMoviesToDB(movieObj, (resp, err) => {
            if (err) {
              console.log(err);
            } else {
              return res.json(resp);
            }
          });
        } else {
          return res.json(data);
        }
      });
    });
  });
}

function checkMoviesInDB(id, cb) {
  var sql = "SELECT * FROM ?? WHERE m_tmdb_id = ?"
  var table = ['movie', id];

  sql = mysql.format(sql, table);

  connection.query(sql, function (err, rows) {
    if (err) {
      return cb(err);
    }

    if (rows.length > 0) {
      return cb(null, {
        success: true,
        update: false,
        message: 'Movies up to date!'
      })
    } else {
      return cb(null, {
        update: true,
        message: 'Updating database!'
      })
    }
  });
}

function insertMoviesToDB(movie, cb) {
  var sql = "INSERT INTO ?? SET ?";
  var table = ['movie', movie];

  sql = mysql.format(sql, table);

  connection.query(sql, function (err, rows) {
    if (err) {
      return cb(err);
    } else {
      return cb(null, {
        success: true,
        message: 'Movie database updated!'
      })
    }
  });
}

function searchTMDB(title) {
  return new Promise((resolve, reject) => {
    https.get(config.tmdbURL + title, response => {
      var body = "";
      response.setEncoding("utf8");

      response.on("data", data => {
        body += data;
      });
      response.on("end", () => {
        body = JSON.parse(body);
        resolve(body.results[0]);
      });
      response.on("error", (err) => {
        reject(err);
      });
    });
  });
}

代码执行后,它会在数据库中插入电影信息,或者使用"电影最新的#34;但我收到此错误并且NodeJS崩溃:

  

错误:发送后无法设置标头。

感谢任何帮助,谢谢!

修改<!/强>

这是新代码,我仍然遇到同样的错误...

exports.checkForMovies = function (req, res) {
  const testFolder = './test/';
  var movieList = [];
  var movieResults = [];

  fs.readdirSync(testFolder).forEach(file => {
    movieList.push(tnp(file));
  });


  var movieObj = movieList.map(movie => {
    var tmp = [];
    return searchTMDB(movie.title).then(data => {
      tmp.push(data);
      return tmp
    });
  });

  var checkDB = Promise.all(movieObj).then(moviesData => {
    moviesData.map(movieData => {
      checkMoviesInDB(movieData[0]).then(checkResponse => {
        if (!checkResponse.movieToInsert) {
          res.json(checkResponse);
        } else {
          var insertArray = checkResponse.movieToInsert;
          var inserting = insertArray.map(movie => {

            var movieObject = {
              m_tmdb_id: movie.id,
              m_name: movie.title,
              m_year: movie.release_date,
              m_desc: movie.overview,
              m_genre: undefined,
              m_poster: movie.poster_path,
              m_watched: 0
            };

            insertMoviesToDB(movieObject).then(insertResponse => {
              res.json(insertResponse);
            });
          });
        }
      });
    });
  });
}

function checkMoviesInDB(movie) {
  var moviesToInsert = [];

  return new Promise((resolve, reject) => {
    var sql = "SELECT * FROM ?? WHERE m_tmdb_id = ?"
    var table = ['movie', movie.id];

    sql = mysql.format(sql, table);

    connection.query(sql, function (err, rows) {
      if (err) {
        return reject(err);
      }

      if (rows.length === 0) {
        moviesToInsert.push(movie);
        resolve({
          success: true,
          movieToInsert: moviesToInsert
        });
      } else {
        resolve({
          success: true,
          message: 'No movie to insert'
        });
      }
    });
  });
}

function insertMoviesToDB(movie) {
  return new Promise((resolve, reject) => {
    var sql = "INSERT INTO ?? SET ?";
    var table = ['movie', movie];

    sql = mysql.format(sql, table);

    connection.query(sql, function (err, rows) {
      if (err) {
        return reject(err);
      } else {
        resolve({
          success: true,
          message: 'Movie added!'
        });
      }
    });
  });
}

function searchTMDB(title) {
  return new Promise((resolve, reject) => {
    https.get(config.tmdbURL + title, response => {
      var body = "";
      response.setEncoding("utf8");

      response.on("data", data => {
        body += data;
      });
      response.on("end", () => {
        body = JSON.parse(body);
        resolve(body.results[0]);
      });
      response.on("error", (err) => {
        reject(err);
      });
    });
  });
}

Auth.js

const config = require('./config');
const jwt = require('jsonwebtoken');

module.exports = function (req, res, next) {

    var token = req.body.token || req.params.token || req.headers['x-access-token'];

    if (token) {
        jwt.verify(token, config.secret, function (err, decoded) {
            if (err) {
                return res.json({
                    success: false,
                    message: 'Failed to authenticate token.'
                });
            } else {
                req.decoded = decoded;
                next();
            }
        });
    } else {
        return res.status(403).send({
            success: false,
            message: 'Please login in to countinue!'
        });
    }

};

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

// Bad Way
const checkForMovies = (req, res) => {
  const movieList = ['Braveheart', 'Highlander', 'Logan'];
  movieList.forEach(movie => {
    res.json(movie);   // Will get Error on second loop: Can't set headers after they are sent.
  })
}

// Good Way
const checkForMovies = (req, res) => {
  const movieList = ['Braveheart', 'Highlander', 'Logan'];
  const payload = { data: { movieList: [] } };
  movieList.forEach(movie => {
    payload.data.movieList.push(movie);
  });
  // send res once after the loop with aggregated data
  res.json(payload);
}

/* GET home page. */
router.get('/', checkForMovies);