-1

I'm setting up a service in node.js which will track URL clicks on a website. I'm having trouble viewing the data in mongoDB. I can pull the document.

_id: ObjectID("5cc0fe18f7875c00bc88de4c")
url:"google.com?ref_source=test"
count:0

Then I parse that string and end up with 0, the count, like expected. However, when I return this count and then log it, I get the undefined error

I've tried to debug the code and stumbled upon a maybe important thing.

The node.js console prints out the URL input, it then prints out undefined which isn't logged if I don't log the result from my function, and then it prints out Retrieved From DB: 0. If I look at my code, Retrieved From DB: 0 should be printed out before undefined. Is this a possible problem?

var MongoClient = require('mongodb').MongoClient;
var mongourl = "mongodb://localhost:27017/";


function count() {
    let string = "reapnow.ml+test"
    lengths = string.split("+")
    console.log(lengths)
    fullurl = (lengths[0] + "?ref_source=" + lengths[1])
    MongoClient.connect(mongourl, function (err, db) {
        var dbo = db.db("analytics")
        var query = {url: fullurl}
        dbo.collection("main").find(query).toArray(function (err, result) {
            let res = JSON.stringify(result)
            res = res.substring(res.indexOf(":") + 1)
            res = res.substring(res.indexOf(":") + 1)
            res = res.substring(res.indexOf(",") + 1)
            res = res.substring(res.indexOf('":') + 2)
            res = res.substring(0, res.indexOf('}]'))
            console.log("Retrieved From DB: " + res)
            //At this point, res is 0
            db.close()
            return (count)
        })
    })
}
console.log(count()) //ouputs the word undefined.

My expected output is 0, not undefined.

Edit 1: Tried using awat/async and am I doing it wrong or is that not the right thing to do?

var MongoClient = require('mongodb').MongoClient;
var mongourl = "mongodb://localhost:27017/";

async function count() {
    let string = "reapnow.ml+test"
    lengths = string.split("+")
    console.log(lengths)
    fullurl = (lengths[0] + "?ref_source=" + lengths[1])
    try{
    var db = await MongoClient.connect(mongourl)
        var dbo = db.db("analytics")
        var query = {url: fullurl}
         var result = await dbo.collection("main").find(query).toArray()
            let res = JSON.stringify(result)
            res = res.substring(res.indexOf(":") + 1)
            res = res.substring(res.indexOf(":") + 1)
            res = res.substring(res.indexOf(",") + 1)
            res = res.substring(res.indexOf('":') + 2)
            res = res.substring(0, res.indexOf('}]'))
            console.log("Retrieved From DB: " + res)
            //At this point, res is 0
            db.close()
            return res
}
catch(error)
{
    return error
}


}
console.log(await count())
Anish Anne
  • 193
  • 3
  • 15
  • 1
    This is more an error of scope for me: here you should see the value of `lengths`, then `"Retrieved From DB: " + res` and nothing else. I dont see the returned value `count` defined anywhere. – Striped Apr 25 '19 at 22:34
  • @Striped: yes, they probably meant `return res`. And it's a "how do I return a value from twice-async call" too. – Sergio Tulentsev Apr 25 '19 at 23:35
  • What's up with all those substrings? Why do you stringify an object and then take the string apart with `substring`? Doesn't make sense. – Sergio Tulentsev Apr 25 '19 at 23:38
  • Yes my bad I meant `return res` I'll clean up the res problem in a bit, I'm just having trouble returning an input. – Anish Anne Apr 26 '19 at 00:47

1 Answers1

-1

var MongoClient = require('mongodb').MongoClient;
var mongourl = "mongodb://localhost:27017/";

async function count() {
    let string = "reapnow.ml+test"
    lengths = string.split("+")
    console.log(lengths)
    fullurl = (lengths[0] + "?ref_source=" + lengths[1])
    try{
 var db = await MongoClient.connect(mongourl)
        var dbo = db.db("analytics")
        var query = {url: fullurl}
         var result = await dbo.collection("main").find(query).toArray()
            let res = JSON.stringify(result)
            res = res.substring(res.indexOf(":") + 1)
            res = res.substring(res.indexOf(":") + 1)
            res = res.substring(res.indexOf(",") + 1)
            res = res.substring(res.indexOf('":') + 2)
            res = res.substring(0, res.indexOf('}]'))
            console.log("Retrieved From DB: " + res)
            //At this point, res is 0
            db.close()
            return count
}
catch(error)
{
 return error
}
        
    
}
console.log(await count())

added try catch and async await in all async calls

shmit
  • 2,306
  • 2
  • 16
  • 20