Shashank S Chandel
Shashank S Chandel

Reputation: 315

Not able to post data to mongo db using express API and node JS(Angular4)

I have successfully completed fetching data from MongoDB using express API and node js and binding it to angular ng2-smart-table. On click of add button of the table, I am calling express API but my post method is not called in api.js, I tried printing console.log inside post but it is not called. For better understanding hereby I post the code:

Component.ts

onCreateConfirm(event,_dataService): void {          
this._dataService.postMutualFunds(event.newData);
event.confirm.resolve(event.newData);
}

data.service.ts

postMutualFunds(parameterValue:any ){
   return this._http.post('/add_mutual_funds', 
   {params:parameterValue}).map(result => this.result = result.json().data);
}

api.js

var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var bodyParser = require("body-parser");

 const connection  = (closure) => {
 return
 MongoClient.connect('mongodb://xxx.com:63898/xx', (err,db) => {
 if(err){
        return console.log(err);
    }
    closure(db);
    });
  }
 //Get call
 router.get('/mutual_funds',(req,res) =>{
    console.log("test get"); //This is executed
    connection((db) => {           
    var projection = {_id:0};
    db.collection('mutual_funds').find().project(projection). 
    toArray().then((mutual_funds) => {
           response.data = mutual_funds;
           res.json(response);
       })
     })      
  })

 //Post call
 router.post('/mutual_funds',(req,res) => {
        console.log(req.body.params);
        console.log("test post");
        db.collection('mutual_funds').insertOne(req.body.params)
 })

  module.exports = router;

enter image description here

Now i am able to call the api js but not able to insert the data to mongodb please find the screenshot below of the api.js exeution output //Why am I not able to post the data?(Updated my post code above) encountering error 500

enter image description here

enter image description here

Upvotes: 2

Views: 400

Answers (2)

Roy G
Roy G

Reputation: 949

You didn't subscribe to the service

onCreateConfirm(event,_dataService): void {          
  this._dataService.postMutualFunds(event.newData).subscribe(    
   (data)=>{
      console.log(data)     
   },
   (err)=>{
     console.log(err)
   }
 )
 event.confirm.resolve(event.newData);
}

The service method returns an Observable of configuration data, the component subscribes to the method's return value. The subscription callback copies the data fields into the component's config object, which is data-bound in the component template for display.

Upvotes: 1

Vaibhav
Vaibhav

Reputation: 1499

why are you not subscribing to the service function call

this._dataService.postMutualFunds(event.newData).subscribe((data)=>{ console.log(data)},
(err)=>console.log(err),
()=>console.log('Observable complete')
);

Upvotes: 0

Related Questions