Updating model data with Schema.pre('save') and http.request calls

Skip to first unread message

Sebastien Couture

Mar 12, 2015, 7:22:04 AM3/12/15
to keyst...@googlegroups.com
Hello to all! 

Total newbie here with intermediate JS experience (although I haven't coded in a few years) and learning Node by building a Keynote site. I have a problem I'm trying to solve in an efficient way and hoping someone here can shed some light. 

I would like to implement a Schema.pre save middleware to update data in my model when I save an item. The middleware should grab a field in my model, make an external API call with that field as a parameter and use part of the response to update an other field in the model. Pretty easy right? 

Here's where it gets somewhat tricky. The external API call is meant to create a new item on the external service. For that to happen, a label parameter needs to be supplied. If it succeeds, it returns the status code 200 and the newly created item in JSON format. If the item already exists however (which may sometimes happen), it returns a 404. Getting an already created item requires a different method call.

Here is what I was able to come up with:

Model.schema.pre('save', function(next) {
var model = this;
function logError(err) {
function saveValue(chunk) {
var obj = JSON.parse(chunk)
if(obj.status == 'success') {
model.otherItem = obj.data.value;

if (model.item) {
var domain = 'domain',
port = 80,
label = 'MY_LABEL_' + model.item,
pathNew = '/api/new?label=' + label + '&api_key=' + process.env.API_KEY,
pathGet = '/api/get?label=' + label + '&api_key=' + process.env.API_KEY,
method = 'GET';

host: domain,
port: port,
path: pathNew,
method: method
}, function(res) {

if( res.statusCode == '404') {
host: domain,
port: port,
path: pathGet,
method: method
}, function(res2) {
res2.on('data', saveValue);
}).on('error', logError).end();

res.on('data', saveValue);

}).on('error', logError).end();



I have 2 questions about this. 

1. How can I get this to work? The console.log() inside saveValue() gets executed but model.otherValue never gets updated.
2. Is there a better way to make these http.request calls. It seems a bit messy and inefficient like this.

Your help is greatly appreciated. I plan to contribute in my own little way by making this project open source and available for others as an example.


Martin Smola

Mar 27, 2015, 12:54:53 PM3/27/15
to keyst...@googlegroups.com
Your problem is that you call next(); before callback of request is called.
Make sure you call the next() after you assign new value to model.otherItem
Reply all
Reply to author
0 new messages