抛出新错误('record not found')语句时的Nodejs和Mongoose错误
Nodejs and Mongoose error at throw new Error('record not found') statement
我是运行Reactjs+Nodejs和Mongodb连接使用mongoose中间件。我有一个声明,用于查找输入的商家 ID 是否存在。如果商户 ID 存在,代码可以正常工作,但是当找不到商户 ID 时,我正在尝试发送错误文本以响应 js。 Nodesjs 中的控件在语句抛出错误时失败 throw new Error('record not found').
请在下面找到我的代码。
const jwt = require('jsonwebtoken')
const Merchant = require('../models/merchant')
const auth = async (req, res, next) => {
try{
const merchantid = await Merchant.findOne({merchantid: req.body.merchantid }) // grab user from database
if (!merchantid) {
throw new Error('record not found')
}
req.merchantid = merchantid
next()
} catch (e) {
res.status(401).send(e)
}
}
module.exports = auth
请查找我在第 8 行收到的错误消息(抛出新错误('record not found'))。
Error: record not found
at auth (C:\Users\Poorna\Desktop\New_folder\Projects\react\React_new_App\temple_receipt_app\server\middleware\auth.js:8:10)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
提前致谢。
这就是 throw 的工作原理。你想 return 像这样
调用系统
改变
throw new Error('record not found')
至
res.status(404)
我是运行Reactjs+Nodejs和Mongodb连接使用mongoose中间件。我有一个声明,用于查找输入的商家 ID 是否存在。如果商户 ID 存在,代码可以正常工作,但是当找不到商户 ID 时,我正在尝试发送错误文本以响应 js。 Nodesjs 中的控件在语句抛出错误时失败 throw new Error('record not found').
请在下面找到我的代码。
const jwt = require('jsonwebtoken')
const Merchant = require('../models/merchant')
const auth = async (req, res, next) => {
try{
const merchantid = await Merchant.findOne({merchantid: req.body.merchantid }) // grab user from database
if (!merchantid) {
throw new Error('record not found')
}
req.merchantid = merchantid
next()
} catch (e) {
res.status(401).send(e)
}
}
module.exports = auth
请查找我在第 8 行收到的错误消息(抛出新错误('record not found'))。
Error: record not found
at auth (C:\Users\Poorna\Desktop\New_folder\Projects\react\React_new_App\temple_receipt_app\server\middleware\auth.js:8:10)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
提前致谢。
这就是 throw 的工作原理。你想 return 像这样
调用系统改变
throw new Error('record not found')
至
res.status(404)