2

I know there are other questions here asking about the same error, but I couldn't find one specific to my situation.

I try to connect with mongo database:

var dbConn = MongoClient.connect('mongodb://localhost', function (err, client) {
  if (err) throw err;
  var db = client.db('mytestingdb');
});

and I get an error on this part of the code:

app.post('/thanks', function(req, res) {
  if (atendees.checkin === req.body.dbstring) {
    dbConn.then(function(db) { //error here
      delete req.body._id; 
      db.collection('feedbacks').insertOne(req.body);
    })

    res.redirect('/thanks.html')
  }
(...)

The idea is to send a form to a database

I tried to follow this tutorial here:

https://programmingmentor.com/post/save-form-nodejs-mongodb/

But the way he tries to connect to the server:

var dbConn = mongodb.MongoClient.connect('mongodb://localhost:27017');

Also gives me an error (db.collection is not a function). From what I've seen here in StackOverflow, that's because of my version of Mongodb. But how can I translate this to the newer version?

Peplm
  • 43
  • 1
  • 8
  • `dbConn` isn't a promise so you can't do `dbConn.then()`. – jfriend00 Mar 23 '18 at 16:10
  • Do you know how can I translate the code at the end (which is a promise) to the newest version of mongodb? – Peplm Mar 23 '18 at 16:12
  • What version of mongodb are you using? – jfriend00 Mar 23 '18 at 16:21
  • I'm using version 3.6.3 – Peplm Mar 23 '18 at 16:27
  • Here's an example: https://stackoverflow.com/questions/35230890/how-to-reuse-mongodb-connection-through-promise – jfriend00 Mar 23 '18 at 16:55
  • This code gives the error: db.collection is not a function. The same as the one in the tutorial – Peplm Mar 23 '18 at 17:05
  • What code? The code in your question does not match the code in the accept answer that I gave you a link to. – jfriend00 Mar 23 '18 at 17:10
  • It's similar to this: https://programmingmentor.com/post/save-form-nodejs-mongodb/ . But that gives "db.collection is not a function" in mongo 3.0+ – Peplm Mar 23 '18 at 17:16
  • In order to help you solve problems with specific code, we have to see THE specific code you're working on. I've already shown you an example that is different than what you show in your question. If you're trying something different and want help with that, you have to show THAT specific code. You can add it to the end of your question (do not change the code you already showed) and then leave a comment here that you've added a new try of code to your question. We can only comment on code we can actually see. That's how it works here. – jfriend00 Mar 23 '18 at 17:25

1 Answers1

1

In mongodb 3.0, you can do this:

const MongoClient = require('mongodb').MongoClient;

MongoClient.connect(url).then(client => {
    const db = client.db(dbName);
    // write code that uses the db here
    db.collection(...).insertOne(...).then(...);
}).catch(err => {
    // error connecting here
});

This example was taken from the 3.0 doc page here: http://mongodb.github.io/node-mongodb-native/3.0/reference/ecmascriptnext/connecting/

Lots of other ES7 examples here: http://mongodb.github.io/node-mongodb-native/3.0/reference/ecmascriptnext/crud/ which should work in current versions of node.js.

jfriend00
  • 683,504
  • 96
  • 985
  • 979