N
N
NataliaOxford2020-01-23 17:25:19
MongoDB
NataliaOxford, 2020-01-23 17:25:19

How to bind a MongoDB collection to NodeJS?

Hello! I can't figure out what's wrong. Here is app.js code:

var express = require("express");

const MongoClient = require('mongodb').MongoClient;
var app = express();
var url = "mongodb://localhost:27017/dbstudents";
app.set("view engine", "hbs");
app.get("/student/:name", function(request, response) {
    var StudentName = request.params.name;
    MongoClient.connect(url, function(err, db) {
        db.collection("students").findOne({
            name: StudentName
        }, function(err, student) {

            if (err) return res.status(400).send();
            console.log(student.telephones);
            db.close();
            response.render("student.hbs", {
                title: student.name,
                phonesVisible: true,
                group: student.group,
                phones: student.telephones
            });

        });
    });
});
app.listen(1000);

When executing the
>node app.js command
, an error is displayed, supposedly the collection is not functioning
Screenshot 1
5e29ac511d33b283870800.png

Although MongoDB sees it
Screenshot 2
5e29ac9a1bf00016825808.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-01-23
@hzzzzl

the easiest way is to use a wrapper over the mongodb driver
https://www.npmjs.com/package/mongoose

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question