Express promise chain error - javascript

I've written a simple express.js server that handles REST API requests and fetches data from a MongoDB database. When I make a GET request to a specific endpoint ("localhost:8081/api/getUserData"), the promise chain doesn't work the way I want it to, and I still don't understand.
This is the error I get:
"[TypeError: Cannot read property 'db' of undefined]"
var MongoClient = require('mongodb').MongoClient;
var express = require('express');
var app = express();
var rp = require("request-promise");
var cors = require('cors');
// use it before all route definitions
app.use(cors({ origin: '*' }));
/********************** REST API FUNCTIONS **********************/
app.get('/api/getUserData', function (req, res, next) {
var context = {};
console.log("in api getUserData")
context.db_url = 'mongodb://localhost:27017/test';
openDatabaseConnection(context)
.then(getAllUserLocations)
.then(closeDatabaseConnection)
.then(function (context) {
res.send(context.userLocations)
})
.catch(function (error) {
console.log("ERROR :");
console.log(error);
})
})
/********************** END REST API FUNCTIONS **********************/
function getAllUserLocations(context) {
context.db.collection("test").find().toArray().then(function (err, result) {
console.log("Received from db: " + result.length + " objects");
context.userLocations = result;
return context;
});
}
function openDatabaseConnection(context) {
console.log("Opening DB connection...");
return MongoClient.connect(context.db_url)
.then(function (db) {
console.log("DB connection opened.");
context.db = db;
return context;
})
}
function closeDatabaseConnection(context) {
console.log("Closing DB connection");
return context.db.close()
.then(function () {
console.log("DB connection closed");
return context;
})
}
/********************** STARTING SERVER **********************/
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Githex server listening at http://%s:%s", host, port)
})
Any help would be appreciated, and even more with an explanation because I don't understand what I've done wrong.
Thanks!

Just like #adeneo mentioned on the first comment, you are missing the db property. Look at your first function:
app.get('/api/getUserData', function (req, res, next) {
var context = {};
console.log("in api getUserData")
context.db_url = 'mongodb://localhost:27017/test';
openDatabaseConnection(context)
.then(getAllUserLocations)
.then(closeDatabaseConnection)
.then(function (context) {
res.send(context.userLocations)
})
.catch(function (error) {
console.log("ERROR :");
console.log(error);
})
});
Now going through the lines within this function:
You setup context as an empty object
You added a db_url property onto the object, so far you have
context = {db_url: "mongodb://localhost:27017/test}
You pass the context object into the openDatabaseConnection function
Within the openDatabaseConnection function, you return a context object. This new returned object doesn't get set anywhere, it just gets returned and never used. You want to call the getuserlocation() function with that returned value.
So instead of just calling
.then(getAllUserConnection)
I would do
.then(function(context){getAllUserConnection(context);})
That should make use of the returned value and make sure you are using it.

Related

API with Node & Express gives back old cached response until newer is retrieved

I cannot find how to prevent this behavior. I have a very simple Express API that works well and gives the intended response. It´s connected to a database storing press articles.
The problem comes when I request article with id 1. It shows up. Then I try to request article with id number 2, and instead I am getting the cached response of the previous article. This goes on for as long as it takes for the database to actually deliver the article 2.
This is what I have:
var sql = require('mssql');
var express = require('express');
var path = require('path');
var response;
var dbConfig = {
//connection details
};
function getArt(an) {
var conn = new sql.Connection(dbConfig);
conn.connect().then(function() {
var req = new sql.Request(conn);
req.query(/* query to the DB */).then(function(recordset) {
response = recordset;
conn.close();
})
.catch(function(err) {
response = err;
conn.close();
});
})
.catch(function(err) {
response = err;
conn.close();
})
return response;
}
var app = express();
app.get('/sourcedeletion/api/v2.0/article/:an', function(req, res) {
res.send(getArt(req.params.an.toString()));
});
app.listen(3000);
What part am I missing, that could help me to get the real response on every request, rather than the previous cached one until the new one arrives from the server?
You're using a global variable response that is not enclosed in the scope of the request. This is prone to error! getArt returns the value of the global variable response without waiting for the promise to execute, so it returns an old value. You should do:
const sql = require('mssql');
const express = require('express');
const path = require('path');
const dbConfig = {
//connection details
};
const getArt = (an) => {
const conn = new sql.Connection(dbConfig);
return conn.connect()
.then(() => {
const req = new sql.Request(conn);
return req.query(/* query to the DB */)
})
.catch(err => err)
.then(response => {
conn.close();
return response;
});
});
};
const app = express();
app.get('/sourcedeletion/api/v2.0/article/:an', (req, res) => {
getArt(req.params.an.toString())
.then(response => {
res.send(response);
});
});
app.listen(3000);
con.connection()
.. is an async call, so before the new response is set, the function returns what ever was there before.
If you want to make sure that the function waits for the connection to finish before returning a value try to change to the following:
return conn.connect().then(function() {
var req = new sql.Request(conn);
return req.query(/* query to the DB */).then(function(recordset) {
response = recordset;
return response;
})
.catch(function(err) {
response = err;
return response;
});
})

passing parameters to module.exports in nodejs

I have the following code for implemetation of nodejs a rest api.
app.js
var connection = require('./database_connector');
connection.initalized(); //guys connection is i want to pass a connection varible to the model
var peson_model = require('./models/person_model')(connection); //this not working
var app = express();
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
app.get('/persons/', function(req, res) {
person_model.get(res); // retrive get results
});
// .............express port and listen
person_model.js is a model class that is supposed to retrieve based on the http verb. For example person.get retrieves the following and currently has a single method as follow.
function Person(connection) {
this.get = function (res) {
connection.acquire(function(err, con) {
con.query('select * from person limit 3', function(err, result) {
con.release();
console.log("get called");
res.send(result);
});
});
};
}
// ** I want to pass a connection variable to the model
module.exports = new Person(connection);
In the code above, var peson_model = require('./models/person_model')(connection); is not working.
How do I pass the connection variable and export the module?
If you return a function from your export, you can pass your parameter.
module.exports = function(connection) {
return new Person(connection);
};
You will need to set this.connection and use that inside your function though.

Registering Glass Timeline Notification with Node

I'm trying to register TAKE_A_NOTE with the 'mirror-api-subscription' event listener. I'm not having any luck: I can launch take a note with "my app", however, there's no console log that the event has been recognized.
I would like to recognize when the TAKE_A_NOTE function has occurred or finished, and handle the response afterwards. I do not know whether the subscription should be on a stream or if I am implementing the EventListener in a faulty manner. Your help would be greatly appreciated.
The code I am utilizing is:
// references
// http://www.recursiverobot.com/post/57348836217/getting-started-with-the-mirror-api-using-node-js
// https://www.npmjs.org/package/mirror-api-subscription
var express = require('express')
, http = require('http')
, https = require('https')
, fs = require('fs')
, googleapis = require('googleapis')
, OAuth2Client = googleapis.OAuth2Client;
var app = express();
var oauth2Client = new OAuth2Client(process.env.MIRROR_DEMO_CLIENT_ID,
process.env.MIRROR_DEMO_CLIENT_SECRET, process.env.MIRROR_DEMO_REDIRECT_URL);
// all environments
app.set('port', 8888);
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
var gotToken = function () {
googleapis
.discover('mirror', 'v1')
.execute(function (err, client) {
if (!!err) {
failure();
return;
}
insertContact(client, failure, success);
insertSubscription(client, failure, success);
});
};
var insertContact = function (client, errorCallback, successCallback) {
client
.mirror.contacts.insert({
"id": "myapp",
"displayName": "myApp",
"priority": 100,
"acceptCommands": [
{"type": "TAKE_A_NOTE"}
],
"speakableName":"my app"
})
.withAuthClient(oauth2Client)
.execute(function (err, data) {
if (!!err)
errorCallback(err);
else
successCallback(data);
});
};
var insertSubscription = function (client, errorCallback, successCallback) {
client.mirror.subscriptions.insert({
"callbackUrl":"https://localhost:7777/notification",
"collection":"timeline",
"userToken":"001",
"verifyToken":"secret",
"operation":["INSERT"]
});
}
var subscription = require('mirror-api-subscription')(
function () {
})
subscription.on('locations#UPDATE',
function (notification, user, payload) {
console.log('location of user %s updated', user.id)
})
subscription.on('timeline#INSERT:LAUNCH',
function (notification, user, payload) {
console.log('subscription timeline#INSERT:LAUNCH')
})
subscription.on('timeline#UPDATE:CUSTOM',
function (notification, user, payload) {
console.log('subscription timeline#UPDATE:CUSTOM')
})
app.post('/notification', subscription.dispatcher())
app.get('/', function (req, res) {
if (!oauth2Client.credentials) {
// generates a url that allows offline access and asks permissions
// for Mirror API scope.
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/glass.timeline'
});
res.redirect(url);
} else {
gotToken();
}
res.write('Glass Mirror API with Node');
res.end();
});
app.get('/oauth2callback', function (req, res) {
// if we're able to grab the token, redirect the user back to the main page
grabToken(req.query.code, failure, function () {
res.redirect('/');
});
});
app.post('/reply', function(req, res){
console.log('replied',req);
res.end();
});
var options = {
key: fs.readFileSync('./ssl/key.pem'),
cert: fs.readFileSync('./ssl/cert.pem'),
};
https.createServer(options, app).listen(7777, function() {
console.log('https listening on 7777');
});
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
There are at least two potential problems with your code:
The callbackUrl must be an internet accessible HTTPS address. "Localhost" isn't good enough, since Google's servers need to be able to resolve it. You can provide an IP address, if you have a public IP address, or use a tunnel.
You don't do anything with the result of the call to client.mirror.subscriptions.insert(). Typically, you should call it the same way you call client.mirror.contacts.insert(), which is to chain it with withAuthClient() and execute(). You'll need to call execute() if you expect it to register with the Mirror service. See https://github.com/google/google-api-nodejs-client/ for documentation about the "googleapis" package and discovery service
Possibly related to #2, but I'm not familiar with the package you're including with require('mirror-api-subscription'), which seems to handle things differently than the "googleapis" package and discovery service it offers. From the reference documentation for it, however, it isn't clear that it actually sets up the callback anywhere and is just there to verify the callbacks and dispatch to functions that do the work.
var insertSubscription = function (client, errorCallback, successCallback) {
client.mirror.subscriptions.insert({
"callbackUrl":"https://mirrornotifications.appspot.com/forward?url=callbackURL",
"collection":"timeline",
"userToken":"001",
"verifyToken":"secret",
"operation":["INSERT"]
}).withAuthClient(oauth2Client).execute(function (err, data) {
if (!!err)
errorCallback(err);
else
successCallback(data);
});
};

Bluebird.js in Node and asynchronous api calls

So I'm trying to build my first webapp with Facebook integration (using facebook-node-sdk). I have it making simple calls to the api, but now it's time to put this all in a simple server and make the calls upon request (this isn't going to be the webapp, itself, but more of an API server).
The problem I'm running into is that, even though I've (presumably) used bluebird to Promisify the Facebook sdk and my makeCall method, I'm still getting "hi" printed and then "undefined" - console.log is getting called before makeCall can return anything.
Here's my app.js:
var Promise = require('bluebird')
, http = require('http')
, Facebook = Promise.promisifyAll(require('facebook-node-sdk'))
, config = require('./config')
, fb = new Facebook({ appId: config.fb.api, secret: config.fb.secret });
var makeCall = new Promise.method(function (username) {
return fb.api(username, function(err, data) {
console.log('hi')
if (err) return err;
return data;
});
});
http.createServer( function (req, res) {
makeCall('/me').then(console.log)
}).listen(8001);
new Promise.method doesn't make sense here (or anywhere since it's a function and not a constructor) nor does makeCall.
Try this:
var Promise = require('bluebird')
, http = require('http')
, Facebook = require('facebook-node-sdk')
, config = require('./config')
, fb = new Facebook({ appId: config.fb.api, secret: config.fb.secret });
Promise.promisifyAll(Facebook.prototype);
http.createServer( function (req, res) {
fb.apiAsync('/me').then(function (data) {
console.log(data)
})
}).listen(8001);
Don't create wrappers when promisifyAll does it for you :)
The problem was that I was neither returning a Promise nor was I resolving said un-returned promise. Here's the fixed code (that works!)
var Promise = require('bluebird')
, http = require('http')
, Facebook = Promise.promisifyAll(require('facebook-node-sdk'))
, config = require('./config')
, fb = new Facebook({ appId: config.fb.api, secret: config.fb.secret });
var makeCall = new Promise.method(function (username) {
return new Promise(function (resolve) {
// resolve
console.log('resolve')
fb.api(username, function(err, data) {
console.log('err: ' + err)
console.log('data: ' + data)
if (err) reject(err);
resolve(data);
});
});
});
http.createServer( function (req, res) {
makeCall('/me').then(function (data) {
console.log(data)
})
}).listen(8001);
Where the output looks like:
resolve
err: null
data: [object Object]
{ id: ... }

express unit testing, calling close on server results in `call of undefined`

I am attempting to test drive an node.js application based on express. I want to return a simple 404.html, which I can successfully do, but afterward, calling close on the node http server gets this error:
Fatal error: Cannot call method 'call' of undefined
I am having a hard time tracking down what is undefined because the same method works beautifully when called elsewhere.
Here is my express code:
function Server() {
this.port = 9000;
this.staticDir = '/public';
}
function handleHomeRequest(req, res) {
var body = '<html><body>Home Page.</body></html>';
res.send(body);
}
Server.prototype.start = function () {
expServer = express();
expServer.get('/', function (req, res) { handleHomeRequest(req, res); });
expServer.use(function (req, res) {
res.status(404).sendfile('./src/public/404.html');
});
runningServer = expServer.listen(this.port);
};
Server.prototype.stop = function (cb) {
runningServer.close(cb);
};
Here is my nodeunit test code:
var ROOT_URL = 'http://localhost',
PORT = 9000,
URL = ROOT_URL + ':' + PORT + '/',
http = require('http'),
Server = require('./server.js'),
server;
exports.setUp = function(done) {
server = new Server();
done();
};
exports.tearDown = function (done) {
server = null;
done();
};
exports['Requesting a page that does not exist results in a 404.'] = function (test) {
server.start();
httpGet(URL + 'guaranteedNotToExistPage', function(res, data) {
test.equal(404, res.statusCode, 'Requesting a page that dne did not return with a status code of 404.');
test.ok(data.indexOf('404 Page Not Found') > -1, 'The 404 page was not returned.');
//test.done();
server.stop(test.done);
});
};
function httpGet(url, callback) {
var request = http.get(url),
receivedData = '';
request.on('response', function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
receivedData += chunk;
});
response.on('end', function () {
callback(response, receivedData);
});
});
}
The result of the http get request come back, the failure only occurs when I call server.stop(test.done); however, stopping the server is required to ensure my unit tests can be run in any order and independent.
First, where runningServer is defined? I can't see a
var runningServer;
anywhere in the first peace of code.
So, if you write a value in prototype.start I doubt you can access it on prototype.stop that is a different scope.
Second, {expressListener}.close() in node 0.6 was just synchronous, they added the callback on the 0.8. So, check the node.js version to be sure that the {cb} is correctly handled.

Categories

Resources