Tuesday 9 May 2017

How to get value from MongoDB?

Some code from main.js:

mongo = require("./mongo.js");
socket.on("storage", function (link) {

        let res = mongo.getStorage(link); //link is a string like 'de43fg3gg3'
        io.emit("updateDOM", res);
    });

File mongo.js:

exports.getStorage = function (randomlink) {
let result;
    Room.findOne({
        link: randomlink
    }, function(err, obj){
    result = obj;
});
   return result.gameProccess; // field which I need
};

But result is undefined. And mongo.getStorage return undefined.

Documents in my collections look like this:

{
"_id": "54g4h65h67j35w4grqhe5",
"link: "m8g2j6z9d6",
"gameProccess": [0,x,0,0,0,x,x,0]
}

I need to get gameProccess field. Also trying this:

let result = Room.findOne({ link: randomlink });
return result;

This:

Room.find({ link: randomlink }, function(err, obj){
return obj[0]["gameProccess"];
});

All variants return undefined. How to return gameProccess field from mongo.getStorage() ?



via user7103883

No comments:

Post a Comment