Meteor Friendly Postgresql

My front end is all Meteor however do the nature of the data, I use a PostgreSQL to store the majority of the bulk of it so I need the server to query and work with the responses.

I am using the npm package pg (https://node-postgres.com) and have been trying to get some functions to query and return but overall, it seems I am running into Meteor not waiting for promises to resolve.

I have tried it with call backs and also wrapping it different ways but I am still getting undefined and trying to see if there is a different way I should be doing this in Meteor. Below is a quick summary is what I am trying to do. I have a function that relies on some queries so when they resolve, I want to then continue on with the function on the server.

serverUpdate = function()
{
 var lastReading = findLastTimestamp()
 if(lastReading.....)
  // Continues on here working with the timestamp
}


findLastTimestamp = function() {
    pool.query("SELECT time FROM sensor_data ORDER BY time DESC LIMIT 1", Meteor.bindEnvironment(function(error, result)
    {
        return result
    }));
}

You are overcomplicating things. Use the promise-enabled syntax instead of the callback syntax.

At the top of your file you will need to add:

import { Promise } from 'meteor/promise';

Then your findLastTimestamp() function should be rewritten like this:

findLastTimestamp = function() {
    return Promise.await(pool.query("SELECT time FROM sensor_data ORDER BY time DESC LIMIT 1"));
}
5 Likes

Wow. Thank you for the sanity check. Have been porting over some functions from an earlier 1.x site with a bunch of wrapAsync and bindEnvironment methods and didn’t even think of the Promise package.

Thanks again for the save.