[英]Not able to create and then save the data into mongo db using express framework(worked now.)
following are my code , It is running well if , I find from database but its only saving IDs into database on create method以下是我的代码,如果我从数据库中找到,它运行良好,但它仅在创建方法时将 ID 保存到数据库中
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
// create application/json parser
app.use(bodyParser.json())
// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: false }))
mongoose.connect('mongodb://localhost/quicks');
const db = mongoose.connection;
const userSchema = mongoose.Schema({
name: String
});
const User = mongoose.model('user', userSchema);
app.get('/', (req, res) => res.send('Hello World!'))
app.post('/createuser', (req, res) => {
let value = {
name : req.body.Name,
email : req.body.Email,
password : req.body.Password
};
User.create(value)
.then((records) => {return res.json(records)})
.catch((err) => {return res.json("err")})
})
app.post('/user', (req, res) => {
let value = {
_id : req.body.ID
};
user.findOne(value)
.then((records) => {return res.json(records)})
.catch((err) => {return res.json("err")})
})
app.listen(4000, () => console.log('Example app listening on port 4000!'))
i am not able to get the data which i have created .我无法获得我创建的数据。 Only the ID is created.仅创建 ID。 what would be the possible error?可能的错误是什么?
In your schema email and password fields are missing.在您的架构中,缺少电子邮件和密码字段。
try this,尝试这个,
app.post('/createuser', (req, res) => {
let value = {
name: req.body.Name,
email: req.body.Email,
password: req.body.Password
};
let model = new User();
model = Object.assign(model, value);
model.save()
.then((records) => {
return res.json(records)
})
.catch((err) => {
return res.json("err")
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.