0

I have written the code to retrieve the data from mongodb using nodejs, but when I try to post that data using angular js, it's not working.

Here is the nodejs code:

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID
var url = 'mongodb://localhost:27017/shru';
var findRestaurants = function(db, callback) {
    var cursor =db.collection('shruthi').find( );
    cursor.each(function(err, doc) {
        assert.equal(err, null);
        if (doc != null) {
            console.dir(doc);
        } else {
            callback();
        }
    });
};
MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    findRestaurants(db, function() {
        db.close();
    });
});

This worked fine, I am able to retrieve the data. My database name is shru and my collection name is shruthi. This is the data what I have retrieved:

{ _id: ObjectID { _bsontype: 'ObjectID', id: 'VÍZrg¸û쮸*�?' },
              name: 'shruts' }
{ _id: ObjectID { _bsontype: 'ObjectID', id: 'VÍZzg¸û쮸*Ñ' },
              name: 'vani' }

When I try to post this data using mongodb its not working fine. Can anyone explain me how to do this? How do I post the above data using angular js?

1 Answer 1

0

Use mongoose for models and db communication. And on top of that create a server that listens to a POST request and then save data in mongo with mongoose.

For a web server I suggest express as a framework.

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

Comments

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.