错误:Route.get() 需要回调函数,但在 app.js 中获得了 [object Undefined]
Posted
技术标签:
【中文标题】错误:Route.get() 需要回调函数,但在 app.js 中获得了 [object Undefined]【英文标题】:Error: Route.get() requires a callback function but got a [object Undefined] at app.js 【发布时间】:2021-01-20 20:56:02 【问题描述】:我不知道为什么我有另一个模型和几乎相同的路由和控制器但不工作,这是消息: 错误:Route.get() 需要回调函数,但得到了 [object Undefined] 在路线。 [as get] (/Users/ediechavero/Documents/Recipe-API/node_modules/express/lib/router/route.js:202:15) 在 Function.proto 中。 [as get] (/Users/ediechavero/Documents/Recipe-API/node_modules/express/lib/router/index.js:510:19) 在对象。 (/Users/ediechavero/Documents/Recipe-API/routes/places-routes.js:22:8) 在 Module._compile (internal/modules/cjs/loader.js:1156:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10) 在 Module.load (internal/modules/cjs/loader.js:1000:32) 在 Function.Module._load (internal/modules/cjs/loader.js:899:14) 在 Module.require (internal/modules/cjs/loader.js:1042:19) 在要求(内部/模块/cjs/helpers.js:77:18) 在对象。 (/Users/ediechavero/Documents/Recipe-API/app.js:62:15) 在 Module._compile (internal/modules/cjs/loader.js:1156:30) 在 Object.Module._extensions..js (在
//Controllers places this is the controllers file for the crud places
const Place = require('../models/Place');
exports.getAllPlacess = async (req, res) =>
try
const places = await Place.find().populate('owner');
res.status(200).json( places );
catch (error)
res.status(400).json( message: `$error` );
;
exports.getPlace = async (req, res) =>
try
const id = req.params;
const place = await (await Place.findById(id)).populate('owner');
res.status(200).json( place );
catch (error)
res.status(400).json( message: `$error` );
;
exports.createPlace = async (req, res) =>
try
const name, typePlace, address = req.body;
const owner = req.user.id;
const place = await Place.create(
name,
typePlace,
address,
owner,
);
res.status(201).json( place );
catch (error)
res.status(400).json( message: `$error` );
;
exports.updatePlace = async (req, res) =>
try
const id = req.params;
const name, typePlace, address = req.body;
const place = await Place.findByIdAndUpdate(id,
name,
typePlace,
address,
owner,
);
res.status(200).json( place );
catch (error)
res.status(400).json( message: `$error` );
;
exports.deletePlace = async (req, res) =>
try
const id = req.params;
await Place.findByIdAndDelete(id);
res.status(200).json( message: 'Deleted Recipe' );
catch (error)
res.status(400).json( message: `$error` );
;
//Route this the file for the routes
const express = require('express');
const router = express.Router();
const uploadConfig = require('../config/cloudinary');
const
getAllPlaces,
getPlace,
createPlace,
updatePlace,
deletePlace,
= require('../controllers/places');
router.post('/upload', uploadConfig.single('photo'), (req, res, next) =>
if (!req.file)
next(new Error('No file uploades'));
res.status(201).json( secure_url: req.file.secure_url );
);
//CRUD PLACES
router.get('/places', getAllPlaces);
router.get('/places/:id', getPlace);
router.post('/places/create', createPlace);
router.put('/places/edit/:id', updatePlace);
router.delete('/places/delete/:id', deletePlace);
module.exports = router;
//app.js
require('dotenv').config();
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const express = require('express');
const mongoose = require('mongoose');
const logger = require('morgan');
const path = require('path');
const colors = require('colors');
const cors = require('cors');
const session = require('express-session');
mongoose
.connect(process.env.DB,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
)
.then((x) =>
console.log(
`Connected to Mongo! Database name: "$x.connections[0].name"`.cyan
.underline.bold
)
)
.catch((err) => console.error('Error connecting to mongo', err.red));
const app_name = require('./package.json').name;
const debug = require('debug')(
`$app_name:$path.basename(__filename).split('.')[0]`
);
const app = express();
app.use(
cors(
credentials: true,
origin: [process.env.FRONTENDPOINT],
)
);
app.use(
session(
resave: false,
saveUninitialized: true,
secret: process.env.SECRET,
cookie: maxAge: 1000 * 60 * 60 ,
)
);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded( extended: false ));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(logger('dev'));
const index = require('./routes/index');
app.use('/', index);
const recipe = require('./routes/recipe-routes');
app.use('/api', recipe);
const place = require('./routes/places-routes');
app.use('/api', place);
// Uncomment this line for production
// app.get('/*', (req, res) => res.sendFile(__dirname + '/public/index.html'));
module.exports = app;
【问题讨论】:
代码的哪一行出现了这个错误? 路由文件 router.get('/places',getAllPlaces) 中的第 22 行 【参考方案1】:您的导出位置和导入位置的拼写似乎有所不同。
您将一个导出定义为:
exports.getAllPlacess = function() ...
但是,然后像这样导入它:
const
getAllPlaces,
getPlace,
createPlace,
updatePlace,
deletePlace,
= require('../controllers/places');
改变这个:
exports.getAllPlacess = function() ...
到这里:
exports.getAllPlaces = function() ...
【讨论】:
@EdieAprovita - 如果这回答了您的问题或解决了您的问题,您可以通过单击答案左侧的复选标记向社区表明这一点。遵循正确的程序,这也将为您赢得一些声誉积分。以上是关于错误:Route.get() 需要回调函数,但在 app.js 中获得了 [object Undefined]的主要内容,如果未能解决你的问题,请参考以下文章
错误:Route.get()需要一个回调函数,但是得到了一个[对象未定义]…它在route / index.js和route / users.js中显示错误
Route.get() 需要一个回调函数但得到一个 [object Object]