(节点:1984)未处理的PromiseRejectionWarning:TypeError:无法读取未定义的属性“电子邮件” 在 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"));
答案 0 :(得分:0)
如果您以 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) =>
希望有帮助!