2

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

1
  • why are you not subscribing to the service function call this._dataService.postMutualFunds(event.newData).subscribe((data)=>{ console.log(data);}); Commented Aug 20, 2018 at 18:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I used your code mentioned above, Now i am able to call the api js but not able to insert the data to mongodb i updated the api.js code and the screenshot provided of output for further details, It will be helpful if you look into this!
First , you don't return nothing for the router use res.json(). second, what is the error details, you check if the data insert successfully to the db
I tried with this code db.collection('mutual_funds').insert(req.body.params,function(err,result){ if (err) { res.send('Error'); console.log(err); } else res.send('Success'); }) I updated the post with the screenshot for error 500 kindly check thank you
it's look like you don't have connection to db. you didn't use connection((db) => {} in this route
I suggest you to create global(singelton) instance that have connection to db
|
0

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')
);

1 Comment

I used your code mentioned above, Now i am able to call the api js but not able to insert the data to mongodb i updated the api.js code and the screenshot provided of output for further details, It will be helpful if you look into this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.