MongoDB : Data not saving via Angular 2 services

2019-09-15 13:42发布

问题:

I have created routes and models as below and data seems to be saved from postman.

UPDATE: Unable to save data after creating Angular services.

I have created a service which will save the product data on click event.

NODE JS CODE

\var\www\html\Express\nodeauthapp-master\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');

...
...
app.use('/products', products);

\var\www\html\Express\nodeauthapp-master\routes\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, user) => {
    if(err){
      res.json({success: false, msg:'Product unable to add'});
    } else {
      res.json({success: true, msg:'Products successfuly added!!'});
    }
     });
    });

module.exports = router;

\var\www\html\Express\nodeauthapp-master\models\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
      },
      product_price: {
        type: String,
        required: true
      },
      product_image: {
        type: String,
        required: false
      },
       product_desc: {
        type: String,
        required: false
     },
       product_status: {
        type: String,
        required: true
     },
       product_createdat: {
        type: String,
        required: true
     },
       product_updatedat: {
        type: String,
       required: true
     }
});

const Products = module.exports = mongoose.model('Products', ProductsSchema);


module.exports.addProduct = function(newProduct, callback){
    newProduct.save(callback);
}

I am basically using this plugin : https://akveo.github.io/ng2-smart-table/#/

URL used : http://10.20.3.44:3000/products/add in postman, i get Success message.

ANGULAR 2 CODE

\src\app\layout\blank-page\blank-page.component.ts

@Component({
selector: 'app-blank-page',
templateUrl: './blank-page.component.html',
styleUrls: ['./blank-page.component.scss']
})
export class BlankPageComponent implements OnInit {

@Input() data: any;


constructor(private addproductService:AddproductService,
            private flashMessage:FlashMessagesService,
            private router: Router) { }
ngOnInit() { }


  onRowSelect(event):void{
    console.log(this.view_update_items_smart_data)
    this.view_update_items_smart_data; 

     this.addproductService.productSave(this.view_update_items_smart_data).subscribe(data => {
   console.log(data)
  if(data.success){


    this.flashMessage.show('Product successfully added', {cssClass: 'alert-success', timeout: 3000});
    this.router.navigate(['/category-items']);
  } else {
    console.log('Failure')
    this.flashMessage.show('Something went wrong', {cssClass: 'alert-danger', timeout: 3000});
    this.router.navigate(['/category-items']);
      }
    });
 }


view_update_items_smart_data = [
 {
  "category_name": "Meals",
  "product_name": "South Meals",
  "product_price": "35.00",
  "product_image":"image1",
  "product_desc": "South Meals",
  "product_status" : "Yes",
  "product_createdat" : "13/7/2017",
  "product_updatedat" : "13/7/2017"
 }
 ]

src\app\shared\services\addproduct.service.ts

@Injectable()
export class AddproductService {

  view_update_items_smart_data: any;

  constructor(private http:Http) { }


  productSave(view_update_items_smart_data){
    console.log('add service')
    let headers = new Headers();
    headers.append('Content-Type','application/json');
    return this.http.post('http://10.22.3.44:3000/products/add', view_update_items_smart_data,{headers: headers})
      .map(res => res.json());
  }
 } 

I get failure message always, but through postman, data gets saved!!

Below is my console log in my browser.