简体   繁体   English

无法使用 express 框架创建然后将数据保存到 mongo db(现在可以使用。)

[英]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.

相关问题 使用Express将文件上传保存到Mongo DB - Save file uploads to Mongo DB using Express 无法使用 express API 和节点 JS(Angular4) 将数据发布到 mongo db - Not able to post data to mongo db using express API and node JS(Angular4) 值错误:未定义,当我尝试使用节点/快速服务器将一些信息保存到 mongo db 集合时 - Error of value: undefined,when i try to save some information to a mongo db collection using a node/express server Nodejs Express 不返回对保存在 mongo 中的数据的响应 - Nodejs Express not return response on data save in mongo 如何在Express JS中使用get方法从mongo db获取数据? - How to fetch data from mongo db using get method in express js? 使用 Express/Node.js 从 Mongo DB 获取数据时出现 404 错误 - Getting 404 error while fetching data from Mongo DB using Express/Node.js 无法使用 React JS 从 Mongo-db 下载 pdf 文件(以二进制数据格式存储) - Not able to download pdf-file (stored in binary data format) from Mongo-db using React JS 尝试使用节点 js 在 mongo db 中保存数据时无法读取未定义的属性“$elemMatch” - Cannot read property '$elemMatch' of undefined while trying to save data in mongo db using node js Node JS 和 Express 无法从 Mongo DB 读取数据 - Node JS and Express cannot read data from Mongo DB 如何使用Sinon对单元测试节点API(Express with Mongo DB) - How to Unit Test a Node API using Sinon (Express with Mongo DB)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM