我使用express创建其余的API。当我向主体发出POST请求时,我没有得到正在发送的数据的响应。我正在获取唯一的objectID,版本和数据未保存在MongoDB数据库中。第一个代码是路由请求API,第二个代码用于架构和模型,第三个代码部分用于运行服务器。在第一部分中,我发出了一个发布请求,该请求会将数据发送到MongoDB并存储在文件中,用于在MongoDB模式中存储数据,并在第二部分中创建模型。代码的第三部分用于运行服务器。
const express = require('express')
const route_ = express.Router()
const proSchema_ = require('../models/productModel')
route_.post('/product', function(req, res, next){
console.log("request",req.body)
proSchema_.create(req.body).then(function(PRODUCT){
res.send(PRODUCT)
}).catch(next)
})
const mongoose = require('mongoose')
const schema = mongoose.Schema
// creating schema & model
const prodSchema = new schema({
pName: String,
quantity: String
});
const Product_ = mongoose.model('product', prodSchema);
module.exports = Product_
const express = require('express')
const app = express()
const Route = require('./router/router')
const bodyParser = require('body-parser')
const urlencodedBodyParser = bodyParser.urlencoded({
extended: true
})
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/curdApp', { useNewUrlParser: true })
mongoose.Promise = global.Promise
app.use(bodyParser.json())
app.use(urlencodedBodyParser)
app.use('/api',Route)
// error handling
app.use(function(err, req, res, next){
res.status(422).send({
error: err.message
})
})
app.listen( process.env.port || 4000, function(){
console.log('Server is listening on port: 4000 ')
})
I want the POST API response with data which I request in the body. I get the response only ObjectID and __v:0. I am getting issue while making a request.