我试图使用邮递员保存猫鼬数据,数据似乎已保存。
但是当我尝试使用API发布时,没有数据被保存。
在 postman 中发布类似的内容:
标题内容类型:application / json
{
"category_name": "Chats",
"product_createdat": "12/7/17",
"product_desc": "juice",
"product_image": "mango_image",
"product_name": "Banana juice",
"product_price": "35",
"product_status": "Active",
"product_updatedat": "12/7/17"
}
\变种\ WWW \ HTML \快递\ nodeauthapp主\ app.js
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database');
// Connect To Database
mongoose.connect(config.database);
// On Connection
mongoose.connection.on('connected', () => {
console.log('Connected to database '+config.database);
});
const app = express();
const products = require('./routes/products');
// Port Number
const port = 3000;
// CORS Middleware
app.use(cors());
// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.json());
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
app.use('/users', users);
app.use('/products', products);
// Index Route
app.get('/', (req, res) => {
res.send('Invalid Endpoint');
});
// Start Server
app.listen(port, () => {
console.log('Server started on port '+port);
});
\变种\ WWW \ HTML \快递\ nodeauthapp主\路由\ products.js
const express = require('express');
const router = express.Router();
var mongoose = require('mongoose');
const config = require('../config/database');
const Products = require('../models/products');
// Add Categories
router.post('/add', (req, res, next) => {
//res.send('Products addition');
//Create a model folder
let newProduct = new Products({
category_name: req.body.category_name,
product_name: req.body.product_name,
product_price: req.body.product_price,
product_image: req.body.product_image,
product_desc: req.body.product_desc,
product_status: req.body.product_status,
product_createdat: req.body.product_createdat,
product_updatedat: req.body.product_updatedat
});
Products.addProduct(newProduct, (err, products) => {
if(err){
res.json({success: false, msg:'Product unable to add'});
} else {
res.json({success: true, msg:'Products successfuly added!!'});
}
});
});
module.exports = router;
更新:
\变种\ WWW \ HTML \快递\ nodeauthapp主\模型\ products.js
const mongoose = require('mongoose');
const config = require('../config/database');
// Products Schema
const ProductsSchema = mongoose.Schema({
category_name: {
type: String
},
product_name: {
type: String,
//required: true //had to change this to default
default: true
},
product_price: {
type: String,
default: true
},
product_image: {
type: String,
default: true
},
product_desc: {
type: String,
default: true
},
product_status: {
type: String,
default: true
},
product_createdat: {
type: String,
required: true
},
product_updatedat: {
type: String,
default: true
}
});
const Products = module.exports = mongoose.model('Products', ProductsSchema);
module.exports.addProduct = function(newProduct, callback){
//newProduct.save(callback);
newProduct.save(function(err) {
// we've updated the dog into the db here if (err) throw err;
});
}
在浏览器中我得到:
成功:false,msg:'产品无法添加'
更新:我在这里有完整的前端代码:
MongoDB : Data not saving via Angular 2 services
在postman中,我正在传递API端点
http://10.22 :3000 /产品/添加