Node.js 执行CRUD操作时,即使存在';这不是承诺

Node.js 执行CRUD操作时,即使存在';这不是承诺,node.js,json,mongoose,postman,Node.js,Json,Mongoose,Postman,我正在尝试使用电子邮件和密码创建一个注册页面。 我对post请求有问题。 每当我通过邮递员发帖时,它总是不断地发送请求。 控制台显示了等待的承诺 function registration() { app.post('/login', (req, res) => { const model = new Model({ email: req.body.email, password: req.body.passwo

我正在尝试使用电子邮件和密码创建一个注册页面。 我对post请求有问题。 每当我通过邮递员发帖时,它总是不断地发送请求。 控制台显示了等待的承诺

function registration()    
{
    app.post('/login', (req, res) => {
       const model = new Model({
            email: req.body.email,
            password: req.body.password
        })
        const result = model.save();
        console.log(result);
    }) 
}
registration();```


//here's the remaing portion

`
const express = require('express');
const app = express();
app.use(express.json());
const mongoose = require('mongoose');
const port = process.env.port || 3000;
app.listen(port, () => console.log(`listening to port ${port}`));

mongoose.connect("mongodb://localhost:27017/Login", { useNewUrlParser: true, useUnifiedTopology: true })
.then(()=>console.log('connected mfs'))
.catch((err) => console.error("error found...", err));

const LoginSchema = new mongoose.Schema({
    email:{type:String, required: true},
    password:{type:String, required :true}
});

const Model = mongoose.model('login', LoginSchema);
.save()
函数返回一个承诺

可能的解决办法

1-使用异步/等待

function registration() {
  app.post('/login', async (req, res) => {
    const model = new Model({
      email: req.body.email,
      password: req.body.password
    })
    const result = await model.save();
    console.log(result);
  })
}
function registration() {
  app.post('/login', async (req, res) => {
    const model = await Model.create({
      email: req.body.email,
      password: req.body.password
    })
    console.log(model)
  })
}
2-使用承诺

function registration() {
  app.post('/login', (req, res) => {
    const model = new Model({
      email: req.body.email,
      password: req.body.password
    })
    model.save().then(result => console.log(result))
  })
}
3-使用回调(但不建议这样做)

您还可以使用
.create()
代替
新模型({})
,然后使用
Model.save()

像这样

1-异步/等待

function registration() {
  app.post('/login', async (req, res) => {
    const model = new Model({
      email: req.body.email,
      password: req.body.password
    })
    const result = await model.save();
    console.log(result);
  })
}
function registration() {
  app.post('/login', async (req, res) => {
    const model = await Model.create({
      email: req.body.email,
      password: req.body.password
    })
    console.log(model)
  })
}
2-承诺

function registration() {
  app.post('/login',  (req, res) => {
    Model.create({
      email: req.body.email,
      password: req.body.password
    })
    .then(model => console.log(model))
  })
}