0

everybody, what I must install for mongodb to work in angular 4?
in my angular 4 project db is object of client of MongoClient class as this code:

MongoClient.connect('mongodb://localhost:27017/test', (err, client) => {
            // Client returned
            var db = client.db('test');});

when I write db.collection() inside this code:

let db = DbClient.connect();
      db.collection();

in app.module.ts file I get this error:

"[ts] Property 'collection' does not exist on type 'void'."

where DbClient is class defined in my database file.
in this project I have nodejs: 8.10.0 mongodb version 2.2.33 and @type/mongodb: 3.0.9, this version of mongodb was 3.0.5 I have downloaded older version after search for solution but still not working yet.
please anybody can help me to solve this error?

2
  • I think you should pass collection name. db.collection('test'); Commented Mar 27, 2018 at 19:19
  • thanks for replay, I think when the error is about argument I must get Expected 1-3 arguments, but got 0, I tried to give the name but not solved. @Krishna Commented Mar 27, 2018 at 19:26

1 Answer 1

3

DbClient.connect(); connects the variable DbClient, and returns nothing. So when you try to write let db = DbClient.connect();, TypeScript correctly alerts you that db is a void variable with no methods.

I think you want:

DbClient.connect();
DbClient.collection();

Or

MongoClient.connect('mongodb://localhost:27017/test', (err, client) => {
    // Client returned
    var db = client.db('test');
    db.collection();
});
Sign up to request clarification or add additional context in comments.

1 Comment

then how I can use the variable db in another class or file in the same project? if not returned any thing?

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.