TypeError [ERR_INVALID_ARG_TYPE]:“ path”参数必须为字符串Node.JS类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError [ERR_INVALID_ARG_TYPE]:“ path”参数必须为字符串Node.JS类型相关的知识,希望对你有一定的参考价值。

[我是Node.js的新手,我尝试使用node.js开发一个简单的电影API应用,并使用express-generator创建了应用。我收到此错误,我无法处理。

首先,我用谷歌搜索,发现我检查了github和stackoverflow,但无法解决我的问题。

首先是这里错误消息:

TypeError [ERR_INVALID_ARG_TYPE]:“ path”参数必须为字符串类型。收到的类型对象在validateString上(internal / validators.js:112:11)在extname(path.js:1231:5)在新视图下(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / view.js:56:14)在Function.render(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / application.js:570:12)在ServerResponse.render(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / response.js:1008:7)在/ Users / tugrul / Projects / Learning Projects / movie-api / app.js:45:7在Layer.handle_error(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / layer.js:71:5)在trim_prefix(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / index.js:315:13)在/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / index.js:284:7在Function.process_params(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / index.js:335:12)在下一个(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / index.js:275:10)在/ Users / tugrul / Projects / Learning Projects / movie-api / app.js:34:3在Layer.handle [作为handle_request](/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / layer.js:95:5)在trim_prefix(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / index.js:317:13)在/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / index.js:284:7在Function.process_params(/ Users / tugrul / Projects / Learning Projects / movie-api / node_modules / express / lib / router / index.js:335:12)

这是我的package.json文件:

{
  "name": "movie-api",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "nodemon ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "mongoose": "^5.9.10",
    "morgan": "~1.9.1"
  }
}

这是我的app.js文件:

const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

const indexRouter = require('./routes/index');
const movieRouter = require('./routes/movie');
const directorRouter = require('./routes/director');


const app = express();

//mongodb connection

const db = require('./helper/db')();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/api/movies', movieRouter);
app.use('api/directors', directorRouter);

// catch 404 and forward to error handler
app.use((req, res, next) => {
  next(createError(404));
});

// error handler
app.use((err, req, res, next) => {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render({error:{message:err.message, code: err.code}});
});

module.exports = app;

这是我的导演路由器文件:

const express = require('express');
const router = express.Router();
//Models

const Director = require('../models/Director');

    // Get All Movies
    router.get('/', (req, res, next) =>{
      res.json({message: 'Director APIs'});
    });


    module.exports = router;

这是我的电影路由器文件:

const express = require('express');
const router = express.Router();
//Models

const Movie = require('../models/Movie');

// Get All Movies
router.get('/', (req, res) =>{
  const promise = Movie.find({ });
  promise.then((data)=>{
    res.json(data);
  }).catch((err)=>{
    res.json(err)
  });
});

//Top 10 List
router.get('/top10', (req, res) =>{
  const promise = Movie.find({ }).limit(10).sort({ imdb_score: -1});
  promise.then((data)=>{
    res.json(data);
  }).catch((err)=>{
    res.json(err)
  });
});

//Add new movie
router.post('/add-movie',(req,res,next)=>{
  //const {title,imdb_score,category,country,year} = req.body;
  const movie = new Movie(req.body);

  const promise = movie.save();

  promise.then((data)=>{
    res.json(data);
  }).catch((err)=>{
    res.json(err);
  });

});


// Get Movie From ID
router.get('/:movie_id',(req,res,next)=>{
  const promise = Movie.findById((req.params.movie_id));
  promise.then((movie)=>{

    if(!movie){
      console.log(movie);
      next({message : 'This movie can not found!', code:404});
    }else{
      res.json(movie);
    }
  }).catch((err)=>{
    res.json(err);
  });

});


//Update Movie
router.put('/:movie_id',(req,res,next)=>{
  const promise = Movie.findByIdAndUpdate(
      req.params.movie_id,
      req.body,
      {
        new: true
      });
  promise.then((movie)=>{

    if(!movie){
      console.log(movie);
      next({message : 'This movie can not found!', code:404});
    }else{
      res.json(movie);
    }
  }).catch((err)=>{
    res.json(err);
  });

});

//Delete movie
router.delete('/:movie_id',(req,res,next)=>{
  const promise = Movie.findByIdAndRemove(
      req.params.movie_id,
      req.body);
  promise.then((movie)=>{

    if(!movie){
      console.log(movie);
      next({message : 'This movie can not found!', code:404});
    }else{
      res.json({status: 1});
    }
  }).catch((err)=>{
    res.json(err);
  });

});

// get movies between two years
router.get('/between/:start_year/:end_year',(req,res,next)=>{
  const {start_year,end_year} = req.params;
  const promise = Movie.find(
      {
        // gte = ">=", lte = "<="
        year: { "$gte": parseInt(start_year), "$lte": parseInt(end_year)}
      }
  );
  promise.then((movie)=>{

    if(!movie){
      console.log(movie);
      next({message : 'This movie can not found!', code:404});
    }else{
      res.json(movie);
    }
  }).catch((err)=>{
    res.json(err);
  });

});


module.exports = router;

[当我尝试访问http://localhost:3000/api/directors时,出现此错误。为什么?我该如何处理?

您能帮我吗?

答案

问题是以下行:

res.render({error:{message:err.message, code: err.code}});

根据docsrender期望第一个参数是要呈现的html页面的路径。假设您的views目录中有一个名为error的文件,将其更改为

res.render('error', {error:{message:err.message, code: err.code}});

应该解决问题。

以上是关于TypeError [ERR_INVALID_ARG_TYPE]:“ path”参数必须为字符串Node.JS类型的主要内容,如果未能解决你的问题,请参考以下文章

反应本机获取多标记[未处理的承诺拒绝:TypeError:TypeError:未定义不是对象(评估'this.state.markers.map

Django TypeError - TypeError: issubclass() arg 1 必须是一个类

pyspark:TypeError:'float'对象不可迭代

Python 3.8 TypeError: can't concat str to bytes - TypeError: a bytes-like object is required, not 's

TypeError: key 必须是一个字符串,一个缓冲区或一个对象在 typeError 与 GCP 文件存在

TypeError: jQueryxxxxxx 不是函数