N
N
NZ012020-01-02 14:15:59
JavaScript
NZ01, 2020-01-02 14:15:59

How to bypass ReferenceError in function?

Hello, I'm new to JS and started to learn MongoDB.

In the process of shortening the code, I came across a ReferenceError in functions .

The code that JS is complaining about:

var express = require('express')
var bodyParser = require('body-parser')
var MongoClient = require("mongodb").MongoClient;

var app = express();

var urlencoderParser = bodyParser.urlencoded({ extended: false })

app.set('view engine', 'ejs');

app.use('/public', express.static('public'))

function new_obj_db(err, result) {
    if(err) {
        return console.log(err)
    }

    console.log(result.ops)
}

var MongoServer = new MongoClient("mongodb://localhost:27017/", { useNewUrlParser: true, useUnifiedTopology: true });
MongoServer.connect(function(err, client){
    var db = client.db("NZcoder")
    var users = db.collection('users')
    var articles = db.collection('articles')

    users.insertOne({name: "Tom", age: 23}, new_obj_db(err, result))

    if(err){
        return console.log(err);
    }

    console.log("Все сработало!")
});


Conclusion:

users.insertOne({name: "Tom", age: 23}, new_obj_db(err, result))
                                                            ^

ReferenceError: result is not defined
    at D:\Olzhas\code\js\tests\test_1 node js\index.js:27:61
    at D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\utils.js:677:5
    at D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\mongo_client.js:226:7
    at connectCallback (D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\operations\connect.js:366:5)
    at D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\operations\connect.js:602:5
    at Object.connectHandler [as callback] (D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\core\sdam\topology.js:297:43)
    at processWaitQueue (D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\core\sdam\topology.js:1047:21)
    at NativeTopology.serverUpdateHandler (D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\core\sdam\topology.js:567:7)
    at Server.emit (node:events:376:20)
    at Monitor.<anonymous> (D:\Olzhas\code\js\tests\test_1 node js\node_modules\mongodb\lib\core\sdam\server.js:151:12)


Question: How to bypass?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DeityLamb, 2020-01-02
@NZ01

insertOne takes the function itself as its second argument, you don't need to call it.

users.insertOne({name: "Tom", age: 23}, new_obj_db)

The method will pass the parameters to it itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question