nodejs sends empty response before data retrieved from mongodb - javascript

When I use jQuery ajax to retrieve data from nodejs (with express4), nodejs sends empty response back before data loaded from mongodb.
This is the message from nodejs console:
GET /query?uid=1 - - ms - -
And this is the error message from Chrome console:
GET http://192.168.1.105:3000/query?uid=1 net::ERR_EMPTY_RESPONSE
I can confirm that data are correctly loaded from mongodb because data can be printed on nodejs console after nodejs sent the empty response. And this is exactly the problem. Why nodejs sends reponse to client before data have been prepared?
I know nodejs is asynchronous and I pay much attention to this great feature, but I still have this problem.
This is my client code:
$.getJSON('/query', {uid:1}, function(response) { console.log('finished!'); });
And this is my server code:
var express = require('express');
var mongodb = require('mongodb');
var GeoJSON = require('geojson');
var strftime = require('strftime');
var router = express.Router();
var MongoClient = mongodb.MongoClient;
router.get('/query', function(req, res, next) {
var url = "mongodb://localhost/example_db";
var collection_name = "example_collection";
var poi = req.query.poi ? req.query.poi.split("||") : null;
var time = req.query.time;
var uid = req.query.uid;
var condition = {};
if (poi) condition.poiname = {$in: poi};
if (time) condition.checkin_time = {$gte:new Date(time.start_time), $lte:new Date(time.end_time)};
if (uid) condition.uid = parseInt(uid);
MongoClient.connect(url, function(err, db) {
if (err) console.log('connection error');
var collection = db.collection(collection_name);
collection.find(condition).sort({checkin_time:1}).toArray(function(err, result) {
if (err) {
console.log(err);
return res.send('error!');
}
if (!result) return res.send('no data');
//This line prints the result after empty response has been sent.
console.log(result);
var data = {};
data['geojson'] = GeoJSON.parse(result, {Point:'loc', include:['poiname', 'uid', 'checkin_time']});
res.json(data);
db.close();
});
});
My data are a little bit large, 12G stored in mongodb. So it usually takes about 3 minutes or more to complete the query. When I use findOne to retrieve only a single document, this is no problem.
Does the data size cause the problem?

Try GeoJSON.parse with callback
var data = {};
GeoJSON.parse(result, {Point:'loc', include:['poiname', 'uid', 'checkin_time']}, function (geojson) {
data['geojson'] = geojson;
res.json(data);
db.close();
});

Related

Cannot read property 'insert' of undefined when trying to put data in MongoDB

I'm getting this error: "Cannot read property 'insert' of undefined" when trying to insert data into a database. The error shows on:
db.coordinates.insert({ "x" : "data.x", "y" : "data.y"})
Database name - "node5"
Collection name - "coordinates"
// Including libraries
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var static = require('node-static'); // for serving files
//db connection
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/node5');
var url = 'mongodb://localhost:27017/node5';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server.");
db.close();
});
// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');
// This is the port for our web server.
// you will need to go to http://localhost:8080 to see it
app.listen(8080);
// If the URL of the socket server is opened in a browser
function handler(request, response) {
request.addListener('end', function () {
fileServer.serve(request, response);
}).resume();
}
// Delete this row if you want to see debug messages
io.set('log level', 1);
// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
// Listen for mouse move events
socket.on('post', function (data) {
console.log('posted');
console.log(data);
socket.broadcast.emit('posted', data); // Broadcasts event to everyone except originating client
db.coordinates.insert({ "x" : "data.x", "y" : "data.y"})
});
});
When writing an answer, please note that I'm new to node.js and I might not understand if you tell the answer in a complex way:)
If you are using monk for your project, then you can drop the mongodb module, since it's functionality is being wrapped up by monk. From Monk's documentation, you should be doing something like:
const monk = require('monk');
const db = monk('localhost:27017/node5')
const coordinates = db.get('coordinates');
Now that you have a reference to your coordinates collection, you can use it later in your code:
coordinates.insert({ x: data.x, y: data.y });
I hope this is easy enough to understand. If it is still confusing, then please comment below and I'll elaborate further :)

Firebase Database updates slowly sometimes not at all

Good morning everyone, I have a question regarding the behavior of my firebase database.
I am populating an end point on my database user with multiple POST requests to my express server app.js from my client like so:
element.on("click", function(){
send request 1 - endpoint1
send request 2 - endpoint2
send request 3 - endpoint3
}
Then in my app.js I have handlers at the routes that use the Request package to make three seperate GET request to the Halo Waypoint API like so:
app.post(endpoint1, function(req,res){
request(HaloAPI + endpoint1, function(error, res, body){
[store data from the response body into an
object to be sent to firebase endpoint 'user']
// User's stats
database + "/user".set(dataObject)
}
}
app.post(endpoint2, function(req,res){
request(HaloAPI + endpoint2, function(error, res, body){
[store data from the response body into an
object to be sent to firebase endpoint 'user'.
This is a url for an image]
//This is a player's emblem
database + "/user/emblem".set(dataObject)
}
}
app.post(endpoint3, function(req,res){
request(HaloAPI + endpoint3, function(error, res, body){
[store data from the response body into an
object to be sent to firebase endpoint 'user'.
This is a url for an image]
//This is a player's Spartan portrait
database + "/user/spartanImage".set(dataObject)
}
}
I'm expecting the database to be updated all at the same time; however, that is not the case all the time. Usually when I click the button with the event listener attached endpoint 2 and 3 come in at the same time as the data from endpoint 1, but most of the time I only get data from only endpoint 2 or only endpoint 3. Eventually I stop getting the data from endpoint 2 and 3 all together. In fact, with this current setup I can see the endpoints user/emblem and user/spartanImage populate in the database console but then they are taken away as soon as they are added. This has been troubling me from the beginning of this project and I would really appreciate some insight from someone more experience in Firebase than myself. Below I will link my relevant code, thanks in advance for the help.
request.js is where the initial AJAX calls are made client-side:
$searchButton.on("click", function(event){
event.preventDefault();
// the value of the search field creates the request object
// it is just a user's Xbox Live Gamertag and is used for the
// the subsequent requests.
var $search = $("#searchField").val();
$.post(homeRoute + "statSearch", {search: $search}, function(data){
console.log(data)
});
firebase.database().ref().on("value", function(snapshot){
function ez(path){
return snapshot.child(path).val();
}
var dataObject = {
gamertag: ez("gamertag"),
totalKills: ez("totalKills"),
totalDeaths: ez("totalDeaths"),
totalGames: ez("totalGames")
};
console.log(dataObject)
});
$.post(homeRoute + "emblemSearch", {search: $search}, function(data){
console.log(data)
});
$.post(homeRoute + "spartanSearch", {search: $search}, function(data){
console.log(data)
});
From there we go over to app.js my express server to make GET requests to the Halo Waypoint API and send the response formatted with the data I want to firebase:
app.post("/statSearch", function(req, res){
var search = req.body.search;
var statsOptions = new Options("https://www.haloapi.com/stats/h5/servicerecords/warzone?players="+search);
request(statsOptions, function (error, response, body) {
if (error) throw new Error(error);
var body = JSON.parse(response.body)
var playerData = {
gamertag: body.Results[0].Id,
totalKills: body.Results[0].Result.WarzoneStat.TotalKills,
totalDeaths: body.Results[0].Result.WarzoneStat.TotalDeaths,
totalGames: body.Results[0].Result.WarzoneStat.TotalGamesCompleted
};
res.send(playerData)
var userRef = ref.child("user");
// sending off the data object to Firebase at the "user" endpoint
userRef.set(playerData);
});
});
//emblem
app.post("/emblemSearch", function(req, res){
var search = req.body.search;
var imgOptions = new Options('https://www.haloapi.com/profile/h5/profiles/'+search+'/emblem', '512');
request(imgOptions, function (error, response, body) {
if (error) throw new Error(error);
var imgString = response.request.uri.href;
res.send(imgString);
var emblemRef = ref.child("user/emblem");
// sending off the url to Firebase at the endpoint "user/emblem"
emblemRef.set(imgString);
});
});
//spartan image
app.post("/spartanSearch", function(req, res){
var search = req.body.search
var spartanOptions = new Options('https://www.haloapi.com/profile/h5/profiles/'+search+'/spartan', '256');
request(spartanOptions, function (error, response, body) {
if (error) throw new Error(error);
var imgString = response.request.uri.href;
res.send(imgString);
var spartanImage = ref.child("user/spartanImage");
// doing the same as emblem to this endpoint^^^^
spartanImage.set(imgString);
});
});

MongoDB : Read a file stored using GridFS from MongoDB in Javascript(using Node.JS)

In a mongodb from javascript code I am able to insert a file(of any extension) using gridfs, It's stored as fs.chunks & fs.files. Now i want to read this file's stream, but on any of the read opration calls on gridStore its giving error : "this.data.buffer.slice is not a function". I have used mongojs library from Node.JS.
// Write a file into MongoDB - Working fine.
// Our file ID
var fileId = new ObjectID();
// Open a new file
var gridStore;
// Create a file and open it
gridStore = new GridStore(db, fileId, "D:/Files/Sofa 2 N050210.3DS", "w+");
var data = fs.readFileSync('D:/Files/Sofa 2 N050210.3DS');
gridStore.open(function (err, gridStore) {
gridStore.write(data, function (err, gridStore) {
// Flush the file to GridFS
gridStore.close(function (err, fileData) {
// Write successfull
});
});
});
Now problem is when reading, After Closing gridStore, I tried to open file for reading & it is giving error at read call i.e on line no-4 in below code.
gridStore.close(function (result) {
var gridStore = new GridStore(db, "test_gs_seek_with_buffer", "r");
gridStore.open(function (err, gridStore) {
gridStore.read(5, function (err, data) {
if(err){
console.log("Error"); return;
}
console.log(data.toString());
});
});
});
Please help me find solution or the way to read back the file stored in GridFS (From the javascript code not from the command prompt).
This code is working fine for me. Try with this. Hope it works.
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
mongoose.connection.once('open', function () {
gfs = new Grid(mongoose.connection.db, mongoose.mongo);//If you are using mongoose
});
var db = new mongo.Db('yourDatabaseName', new mongo.Server("127.0.0.1", 27017)); //if you are using mongoDb directily
var gfs = Grid(db, mongo);
var rstream = gfs.createReadStream(filename);
var bufs = [];
rstream.on('data', function (chunk) {
bufs.push(chunk);
}).on('error', function () {
res.send();
})
.on('end', function () { // done
var fbuf = Buffer.concat(bufs);
var File = (fbuf.toString('base64'));
res.send(File);
});

How to execute the function return the value to called function across node js files

I have 3 node js files :
mysqlconnection.js to store the database connection properties:
var mysql = require('mysql');
var cjson = require('cjson');
var yaml_config = require('node-yaml-config');
// project files
var config = yaml_config.load(__dirname + '/billingv2.yaml');
exports.execute = function(callback){
var connection = mysql.createConnection(
{
host : config.host,
user : config.user,
password : config.password,
database : config.database,
}
);
connection.connect();
return callback(null,connection);
}
subscriptionRestService.js to handle the REST api calls:
var express = require('express');
var app = express();
app.use(express.bodyParser());
var fs = require('fs');
// Project files
var mysql = require('./mysqlRestService.js');
// Get Resource Subscription data by Resourceuri
app.post('/pricingdetails', function(req, res) {
var workload = req.body;
if(workload.elements && workload.elements.length > 0)
{
var arr = [];
for(var index in workload.elements)
{
arr[index] = workload.elements[index].uri;
}
var resourceIdentifiers = arr.join(',');
}
console.log(resourceIdentifiers);
mysql.getPricingDetail(function(resourceIdentifiers,callback){
});
});
mysqlRestService.js to handle mysql queries/stored procedures:
// packages
var mysql = require('mysql');
var cjson = require('cjson');
var fs = require('fs');
var yaml_config = require('node-yaml-config');
// project files
var dbconnection = require('./mysqlconnection');
exports.getPricingDetail = function (resourceIdentifiers,callback){
console.log('entered into mysql function');
console.log(resourceIdentifiers);
var pricingDetail = {};
dbconnection.execute(function(err,response){
if(err){
throw err;
}
else
{
var selectqueryString = "call SP_ExposePricingDetailforUI('" + resourceIdentifiers + "')";
response.query(selectqueryString, function(err,pricingDetail){
if(err) {
throw err;
}
else
{
console.log(pricingDetail);
pricingDetail = pricingDetail;
}
});
}
});
//console.log('printing pricing details');
//console.log(pricingDetail);
};
problems faced
Unable to send the variable resourceIdentifiers from subscriptionRestService to mysqlRestService.js
Unable to return the pricingdetail from mysqlRestService.js to calling function in subscriptionRestService.
Any guidance greatly appreciated.
Unable to send the variable resourceIdentifiers from subscriptionRestService to mysqlRestService.js
Well, you didn't send it. It currently is a parameter of your callback function in the invocation, not an argument for the parameter of getPricingDetails. Use
mysql.getPricingDetail(resourceIdentifiers, function callback(result){
// use result here
});
Unable to return the pricingdetail from mysqlRestService.js to calling function in subscriptionRestService.
I've got no idea what pricingDetail = pricingDetail; was supposed to do. You have to call (invoke) back the callback here! Use
callback(pricingDetail);

How can I set req.session from this scope?

I'm writing a Node.js application using Express and a PostgreSQL database using node-postgres. I want to look up the current user's username and real name based on their email, and set them in req.session. However, if I set them where I am in the code below, they are undefined when we leave that block (i.e. the first console.log statements print the correct info, the second set prints undefined. How can I solve this?
var client = new pg.Client(app.conString);
var realname = "";
var username = "";
client.connect();
var query = client.query(
"SELECT * FROM users WHERE email = $1;",
[req.session.email]
);
query.on('row', function(row) {
req.session.realname = row.realname;
req.session.username = row.username;
console.log(req.session.realname);
console.log(req.session.username);
});
console.log(req.session.realname);
console.log(req.session.username);
query.on('end', function() {
client.end();
});
The second pair of console.log will execute before the query-results are available (in the row event handler).
If your code is going to be used in an Express route, you would use something like this:
app.get('/', function(req, res) {
var client = new pg.Client(app.conString);
var realname = "";
var username = "";
client.connect();
var query = client.query(
"SELECT * FROM users WHERE email = $1;",
[req.session.email]
);
query.on('row', function(row) {
req.session.realname = row.realname;
req.session.username = row.username;
});
query.on('end', function() {
client.end();
res.send(...); // <-- end the request by sending back a response
});
});
An alternative for using the EventEmitter interface for node-postgres would be to just pass a callback to query (which looks better with Express IMHO):
client.query(
"SELECT * FROM users WHERE email = $1;",
[req.session.email],
function(err, results) {
if (err)
// handle error
else
if (results.length)
{
req.session.realname = results[0].realname;
req.session.username = results[0].username;
}
res.send(...); // done
});

Categories

Resources