Model.schema.pre('save', function(next) {
var model = this;
function logError(err) {
console.log(err)
}
function saveValue(chunk) {
var obj = JSON.parse(chunk)
if(obj.status == 'success') {
console.log(obj);
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';
http.request({
host: domain,
port: port,
path: pathNew,
method: method
}, function(res) {
res.setEncoding('utf8');
if( res.statusCode == '404') {
http.request({
host: domain,
port: port,
path: pathGet,
method: method
}, function(res2) {
res2.setEncoding('utf8');
res2.on('data', saveValue);
}).on('error', logError).end();
}
res.on('data', saveValue);
}).on('error', logError).end();
}
next();
});