UnhandledPromiseRejectionWarning - UnhandledPromiseRejectionWarning - DeprecationWarning

我收到此错误 ->

(node:18420) UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“名称”

at C:\Users\ohrid\Desktop\backend2\routes\categories.js:27:24
at Layer.handle [as handle_request] (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:281:22     
at Function.process_params (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:317:13)
at C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:284:7       
at Function.process_params (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\ohrid\Desktop\backend2\node_modules\express\lib\router\index.js:275:10)
at logger (C:\Users\ohrid\Desktop\backend2\node_modules\morgan\index.js:144:5)

(node:18420) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。要在未处理的承诺拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1)

(node:18420) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程。

我的路线/categories.js:

const { Category } = require('../models/category')
const express = require('express')
const router = express.Router()
 
router.get(`/`,async (req,res) => {
    const categoryList = await Category.find()
 
    if (!categoryList) {
        res.status(500).json({ success: false })
    }
    res.status(200).send(categoryList)
})
 
router.get('/:id',res) => {
    const category = await Category.findById(req.params.id)
 
    if (!category) {
        res.status(500).json({
            message: 'The category with the given ID was not found.',})
    }
    res.status(200).send(category)
})
 
router.post('/',res) => {
    let category = new Category({
        name: req.body.name,icon: req.body.icon,color: req.body.color,})
    category = await category.save()
 
    if (!category)
        return res.status(400).send('the category cannot be created!')
 
    res.send(category)
})
 
router.put('/:id',res) => {
    const category = await Category.findByIdAndUpdate(
        req.params.id,{
            name: req.body.name,icon: req.body.icon || category.icon,},{ new: true }
    )
 
    if (!category)
        return res.status(400).send('the category cannot be created!')
 
    res.send(category)
})
 
router.delete('/:id',(req,res) => {
    Category.findByIdAndRemove(req.params.id)
        .then((category) => {
            if (category) {
                return res
                    .status(200)
                    .json({
                        success: true,message: 'the category is deleted!',})
            } else {
                return res
                    .status(404)
                    .json({ success: false,message: 'category not found!' })
            }
        })
        .catch((err) => {
            return res.status(500).json({ success: false,error: err })
        })
})
 
module.exports = router

我的 app.js

const express = require('express')
const app = express()
const morgan = require('morgan')
const mongoose = require('mongoose')
const cors = require('cors')
const dotenv = require('dotenv')
require('dotenv/config')
 
app.use(cors())
app.options('*',cors())
 
//middleware
app.use(morgan('tiny'))
 
//Routes
const categoriesRoutes = require('./routes/categories')
const productsroutes = require('./routes/products')
const usersRoutes = require('./routes/users')
const ordersRoutes = require('./routes/orders')
 
const api = process.env.API_URL
 
app.use(`${api}/categories`,categoriesRoutes)
app.use(`${api}/products`,productsroutes)
app.use(`${api}/users`,usersRoutes)
app.use(`${api}/orders`,ordersRoutes)
 
mongoose
    .connect(
        'mongodb+srv://dani:Luka5678@cluster0.23wee.mongodb.net/e-shop?retryWrites=true&w=majority',{
            useNewUrlParser: true,useUnifiedTopology: true,dbName: 'e-shop',}
    )
    .then(() => {
        console.log('Database connection is ready')
    })
    .catch((err) => {
        console.log(err)
    })
 
app.listen(4000,() => {
    console.log('server is running on http://localhost:4000')
})

我应该改变什么?

a125000178 回答:UnhandledPromiseRejectionWarning - UnhandledPromiseRejectionWarning - DeprecationWarning

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1763.html

大家都在问