我不知道为什么会出现这个错误,我有另一个模型和几乎相同的路由和控制器,但没有工作,这是消息:error:route.get()需要一个回调函数,但得到了一个[对象未定义]在路由。[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:115630)在object.module._extensions.js(internal/modules/cjs/loader.js:1176:10)在module.load(internal/modules/cjs/loader.js:1000:32)在function.module._load((/users/ediechavero/documents/recipe-api/app.js:62:15),位于object.module._extensions..js的module._compile(internal/modules/cjs/loader.js:1156:30
//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;
它看起来有一个拼写不同的地方,你导出和你导入。
您正在将一个导出定义为:
exports.getAllPlacess = function() { ... }
但是,然后将其导入为:
const {
getAllPlaces,
getPlace,
createPlace,
updatePlace,
deletePlace,
} = require('../controllers/places');
更改此内容:
exports.getAllPlacess = function() { ... }
对此:
exports.getAllPlaces = function() { ... }
我想将router.get()与sql查询中的查询分开,但我出现了这个错误,当我将direct函数声明为router.get()函数的参数时,它可以工作。 user.js UserQueries.js 错误:route.get()需要回调函数,但在route.(anonymous function)处获得[object Undefined](/Home/Antonio/AchaiAPI/Node_
我得到了 错误:Route.Get()需要一个回调函数,但得到了一个[object Undefined] 这是我的路由文件: 错误日志: 错误:route.get()需要回调函数,但在route.(anonymous function)[as get](C:\users\PAI\DesktoP\mean\node_modules\express\lib\router\route.js:202:15
当尝试ViewChild时,我会得到错误。错误为“未提供'opts'的参数。” @ViewChild都给出了错误。 ts(11,2):错误TS2554:应为2个参数,但得到1。
问题内容: 我正在尝试使用jQuery和Ajax,并且使用了这种方法。但是我收到错误$ .toJSON不是firebug中的函数。问题出在哪里?我使用jquery 1.3.2。谢谢 问题答案: 没错 没有功能:http : //api.jquery.com/jQuery.toJSON。也许您想使用它。
您好,我正在NodeJS下编写应用程序,但出现以下错误: 抛出新的TypeError('Router.use()需要一个中间件函数,但得到了一个'gettype(fn))^ TypeError: Router.use()需要一个中间件函数,但在Function.use(C:\User\decopiapo\restserver1221\node_modules\Express\lib\router\