gpt4 book ai didi

node.js - 在 cmd 和本地主机 "createError is not defined"上得到这个输出

转载 作者:行者123 更新时间:2023-12-01 23:51:26 25 4
gpt4 key购买 nike

大家好,我是 node js、mongo db 的新手。我使用此链接创建了一个 crud 应用程序:https://www.positronx.io/mean-stack-tutorial-angular-7-crud-bootstrap/ .我不知道,但为什么我在 cmd 和本地主机上得到这个输出“createError is not defined”。请帮助我,因为我是 Node 的初学者。

server.js

let express = require('express'),
path = require('path'),
mongoose = require('mongoose'),
cors = require('cors'),
bodyParser = require('body-parser'),
dbConfig = require('./database/db');

// Connecting with mongo db
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/meandb", {
useNewUrlParser: true
}).then(() => {
console.log('Database sucessfully connected')
},
error => {
console.log('Database could not connected: ' + error)
}
)

// Setting up port with express js
const employeeRoute = require('../backend/routes/employee.route')
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cors());
app.use(express.static(path.join(__dirname, 'dist/mean-stack-crud-app')));
app.use('/', express.static(path.join(__dirname, 'dist/mean-stack-crud-app')));
app.use('/api', employeeRoute)

// Create port
const port = process.env.PORT || 4000;
const server = app.listen(port, () => {
console.log('Connected to port ' + port)
})

// Find 404 and hand over to error handler
app.use((req, res, next) => {
next(createError(404));

});

// error handler
app.use(function (err, req, res, next) {

console.error(err.message); // Log error message in our server's console
if (!err.statusCode) err.statusCode = 500; // If err has no specified error code, set error code to 'Internal Server Error (500)'
res.status(err.statusCode).send(err.message); // All HTTP requests must have a response, so let's send back an error with its status code and message
});

Employee.route.js

const express=require('express');
const app=express();
const employeeRoute=express.Router();

//Employee model
let Employee=require('../models/Employee');

//Add Employee
employeeRoute.route('/create').post((req,res,next)=>{
Employee.create(req.body,(error,data)=>{
if(error){
return next(error)
}
else{
res.json(data)
}
})
});

//Get ALL Employee
employeeRoute.route('/').get((req,res)=>{
Employee.find((error,data)=>{
if(error){
return next(error)
}
else{
res.json(data)
}
})
})
// Get single employee
employeeRoute.route('/read/:id').get((req, res) => {
Employee.findById(req.params.id, (error, data) => {
if (error) {
return next(error)
} else {
res.json(data)
}
})
})


// Update employee
employeeRoute.route('/update/:id').put((req, res, next) => {
Employee.findByIdAndUpdate(req.params.id, {
$set: req.body
}, (error, data) => {
if (error) {
return next(error);
console.log(error)
} else {
res.json(data)
console.log('Data updated successfully')
}
})
})

// Delete employee
employeeRoute.route('/delete/:id').delete((req, res, next) => {
Employee.findOneAndRemove(req.params.id, (error, data) => {
if (error) {
return next(error);
} else {
res.status(200).json({
msg: data
})
}
})
})
module.exports=employeeRoute;

在角度部分,当我尝试提交数据时,提交按钮什么也没做。

最佳答案

需要一些时间来适应,但此错误通常意味着您没有在别处创建名为“createError”的变量。该变量通常用于“http-errors”npm 包。您可以在此处了解更多信息:https://www.npmjs.com/package/http-errors

将其添加到您的 app.js 文件的顶部。

App.js

const createError = require('http-errors');

还要确保您已使用此命令安装并保存了软件包。在与您的 package.json 文件相同的目录中运行它。

npm install http-errors --save

关于node.js - 在 cmd 和本地主机 "createError is not defined"上得到这个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63411757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com