.cache() 不是函数 Mongoose
.cache() is not a function Mongoose
我刚刚使用 Nodejs、Express 和 Mongoose 创建了一个简单的联系表单后端。我使用了 Pug 模板引擎。我创建了一个后端来通过联系表单从用户那里获取数据并使用 Mongoose 保存到我的数据库中。但是我收到以下错误。
TypeError: myData.save(...).then(...).cache is not a function
我的App.js
const express = require("express");
const path = require("path")
const fs = require("fs")
const app = express();
const mongoose = require('mongoose');
const bodyparser = require('body-parser')
mongoose.connect('mongodb://localhost/contact', { useNewUrlParser: true, useUnifiedTopology: true });
const port = 3000;
//define mongoose schema
const contactSchema = new mongoose.Schema({
name: String,
phone: String,
age: String,
email: String,
gender: String,
address: String,
});
const Contact = mongoose.model('Contact', contactSchema);
//Express Specific Stuff
app.use('/static', express.static('static'))
app.use(express.urlencoded())
//Set the template Engine as Pug
app.set('view engine', 'pug')
app.set('views', path.join(__dirname, 'views'))//Set the views directory
//Endpoints
app.get("/", (req, res) => {
res.status(200).render(`index.pug`)
})
app.post('/', (req, res) => {
var myData = new Contact(req.body);
myData.save().then(()=>{
res.send("This Data has been saved")
}).cache(()=>{
res.status(400).send("Item was not saved into DB")
})
})
app.listen(port, () => {
console.log(`The application started succefully ${port}`)
});
我的index.pug
doctype html
head
meta(charset='UTF-8')
meta(http-equiv='X-UA-Compatible' content='IE=edge')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
title Contact
style.
@import url('https://fonts.googleapis.com/css2?family=Baloo+Tammudu+2&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Baloo Tammudu 2', cursive;
}
.contact {
max-width: 60%;
/* text-align: center; */
/* display: flex;*/
position: relative;
left: 20%;
align-items: center;
}
input{
height: 40px;
font-size: 15px;
padding: 7px 5px;
}
textarea{
font-size: 15px;
padding: 7px 5px;
}
form{
display: flex;
flex-direction: column;
}
.btn{
background-color: black;
max-width: 70px;
color: white;
text-align: center;
border-radius: 10px;
cursor: pointer;
}
button{
margin: 10px;
padding-top: 5px;
}
h1{
display: flex;
align-items: center ;
}
button a{
text-decoration: none;
color: white;
}
.contact
form(action='/' method='post')
h1 Membership Form
label(for='name') Full Name
input#name(type='text' name='name' placeholder='Enter your name')
label(for='age') Age
input#age(type='number' name='age' placeholder='Enter your age')
label(for='email') Email
input#email(type='email' name='email' placeholder='Enter your Email Id')
label(for='gender') Gender
input#gender(type='text' name='gender' placeholder='Enter your Gender')
label(for='address') Address
textarea#address(name='address' cols='30' rows='05' placeholder='Enter your Address')
button.btn Submit
我刚刚使用 Nodejs、Express 和 Mongoose 创建了一个简单的联系表单后端。我使用了 Pug 模板引擎。我创建了一个后端来通过联系表单从用户那里获取数据并使用 Mongoose 保存到我的数据库中。但是我收到以下错误。
请帮我。提前致谢。
你应该使用 catch 而不是缓存。
对于额外的上下文,catch 是一种可用于 promise 的方法,用于捕获抛出的错误或被拒绝的 promises。
缓存是保存临时数据以帮助提高性能等的术语。
我刚刚使用 Nodejs、Express 和 Mongoose 创建了一个简单的联系表单后端。我使用了 Pug 模板引擎。我创建了一个后端来通过联系表单从用户那里获取数据并使用 Mongoose 保存到我的数据库中。但是我收到以下错误。
TypeError: myData.save(...).then(...).cache is not a function
我的App.js
const express = require("express");
const path = require("path")
const fs = require("fs")
const app = express();
const mongoose = require('mongoose');
const bodyparser = require('body-parser')
mongoose.connect('mongodb://localhost/contact', { useNewUrlParser: true, useUnifiedTopology: true });
const port = 3000;
//define mongoose schema
const contactSchema = new mongoose.Schema({
name: String,
phone: String,
age: String,
email: String,
gender: String,
address: String,
});
const Contact = mongoose.model('Contact', contactSchema);
//Express Specific Stuff
app.use('/static', express.static('static'))
app.use(express.urlencoded())
//Set the template Engine as Pug
app.set('view engine', 'pug')
app.set('views', path.join(__dirname, 'views'))//Set the views directory
//Endpoints
app.get("/", (req, res) => {
res.status(200).render(`index.pug`)
})
app.post('/', (req, res) => {
var myData = new Contact(req.body);
myData.save().then(()=>{
res.send("This Data has been saved")
}).cache(()=>{
res.status(400).send("Item was not saved into DB")
})
})
app.listen(port, () => {
console.log(`The application started succefully ${port}`)
});
我的index.pug
doctype html
head
meta(charset='UTF-8')
meta(http-equiv='X-UA-Compatible' content='IE=edge')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
title Contact
style.
@import url('https://fonts.googleapis.com/css2?family=Baloo+Tammudu+2&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Baloo Tammudu 2', cursive;
}
.contact {
max-width: 60%;
/* text-align: center; */
/* display: flex;*/
position: relative;
left: 20%;
align-items: center;
}
input{
height: 40px;
font-size: 15px;
padding: 7px 5px;
}
textarea{
font-size: 15px;
padding: 7px 5px;
}
form{
display: flex;
flex-direction: column;
}
.btn{
background-color: black;
max-width: 70px;
color: white;
text-align: center;
border-radius: 10px;
cursor: pointer;
}
button{
margin: 10px;
padding-top: 5px;
}
h1{
display: flex;
align-items: center ;
}
button a{
text-decoration: none;
color: white;
}
.contact
form(action='/' method='post')
h1 Membership Form
label(for='name') Full Name
input#name(type='text' name='name' placeholder='Enter your name')
label(for='age') Age
input#age(type='number' name='age' placeholder='Enter your age')
label(for='email') Email
input#email(type='email' name='email' placeholder='Enter your Email Id')
label(for='gender') Gender
input#gender(type='text' name='gender' placeholder='Enter your Gender')
label(for='address') Address
textarea#address(name='address' cols='30' rows='05' placeholder='Enter your Address')
button.btn Submit
我刚刚使用 Nodejs、Express 和 Mongoose 创建了一个简单的联系表单后端。我使用了 Pug 模板引擎。我创建了一个后端来通过联系表单从用户那里获取数据并使用 Mongoose 保存到我的数据库中。但是我收到以下错误。 请帮我。提前致谢。
你应该使用 catch 而不是缓存。
对于额外的上下文,catch 是一种可用于 promise 的方法,用于捕获抛出的错误或被拒绝的 promises。
缓存是保存临时数据以帮助提高性能等的术语。