(节点:1984)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“电子邮件”[重复]
Posted
技术标签:
【中文标题】(节点:1984)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“电子邮件”[重复]【英文标题】:(node:1984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'email' of undefined [duplicate] 【发布时间】:2021-09-18 22:20:24 【问题描述】:(node:1984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'email' of undefined 在 app.use (C:\Users\niko\Desktop\test\app.js:15:24) 在 Layer.handle [as handle_request] (C:\Users\niko\Desktop\test\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:335:12) 在下一个(C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:275:10) 在 expressInit (C:\Users\niko\Desktop\test\node_modules\express\lib\middleware\init.js:40:5) 在 Layer.handle [as handle_request] (C:\Users\niko\Desktop\test\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\niko\Desktop\test\node_modules\express\lib\router\index.js:284:7 (节点:1984) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号:2)
const express = require('express');
const mongodb = require('mongodb');
const uri = 'mongodb:http://localhost:27017/moneymarket'
const MongoClient = require('mongodb').MongoClient();
const app = express();
mongodb.connect(uri, useNewUrlParser: true, useUnifiedTopology: true , () =>
console.log("connected to DB");
);
app.use('/', async (req, res, next) =>
const email = req.body.email;
console.log(User)
//const email = req.body.email;
const emailExist = await User.findOne(email: req.body.email);
if (emailExist)
return res.send("email already exists")
);
app.listen(3000, () => console.log("Server Runing on localhost:3000"));
【问题讨论】:
【参考方案1】:如果您以 JSON 格式发送数据,则需要添加 JSON 正文解析器:
// enable json body parsing
app.use(express.json());
// if change this to actually a post/put, something that has a body
app.post('/', async (req, res, next) =>
希望对您有所帮助!
【讨论】:
以上是关于(节点:1984)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“电子邮件”[重复]的主要内容,如果未能解决你的问题,请参考以下文章