Hi,
I would like to use this bluebird code:
How can I use bluebird in meteor, without need to do extra stuff like solving the bind environment errors?
Complete code here
import { Meteor } from 'meteor/meteor';
import { http } from 'meteor/meteor';
import { Apps, TemplateApps } from '/imports/api/apps.js';
import { Streams } from '/imports/api/streams.js';
import { config } from '/imports/api/config.js';
import { engineConfig } from '/imports/api/config.js';
import { certs } from '/imports/api/config.js';
var fs = require('fs');
var qsocks = require('qsocks');
var QRS = require('qrs');
import { Promise } from 'meteor/promise';
var qrs = null;
var generatedAppGuid = '';
function getApps(){
console.log('server: getApps');
return qsocks.Connect(engineConfig).then(function(global) {
//We can now interact with the global class, for example fetch the document list.
//qsocks mimics the Engine API, refer to the Engine API documentation for available methods.
global.getDocList().then(function(docList) {
// docList.forEach(function(doc) {
// console.log(doc.qDocName);
// });
return docList;
});
});
};
function generateStreamAndApp (customers) {
console.log('METHOD called: generateStreamAndApp for the template apps as stored in the database of the fictive OEM');
// check(customers, Array);
//for each customer
//create stream if not already exist
//copy app
//publish to stream
//add 'generated' tag
var templateApps = TemplateApps.find().fetch();
if(!TemplateApps.find().count()){//user has not specified a template
throw new Meteor.Error('No Template', 'user has not specified a template for which apps can be generated');
}
if(!customers.length){ // = 0
throw new Meteor.Error('No customers', 'user has not specified at least one customer for which an app can be generated');
}
// console.log('customers are', customers);
// console.log('template apps are',templateApps);
// console.log('2: the templateAppGuid are',templateAppsGuids);
//FOR EACH TEPMPLATE
var generationPromise = Promise.all(_.map(templateApps, function(templateApp){
console.log('the current template app: ', templateApp.name);
return Promise.all(_.map(customers, function(customer){
//FOR EACH CUSTOMER
console.log('############## START CREATING THE TEMPLATE '+templateApp.name+' FOR THIS CUSTOMER: '+customer.name);
var stream = Streams.findOne({name:customer.name}); //Find the stream for the name of the customer in Mongo, and get his Id from the returned object
if(stream){
var streamId = stream.id;
}
// console.log('!!! The stream Id found is: ',streamId);
var myPromise = new Promise(function(resolve, reject) {
if(!streamId){
console.log('No stream for customer exist, so create one: '+customer.name);
createStream( customer.name)
.then(
function fulfilled (result) {
// console.log('stream create promosi fulfilled, result of create stream promise', result);
streamId = result.id;
resolve('created stream with id '+result.id);
},
function Rejected (error){
// console.error('Promise Rejected: Error when trying to copy the app', error);
reject('Promise Rejected: Error when trying to create a stream');
})
} else{
// console.log('No need to create a stream, because it already exists: '+Streams.findOne({name:customer.name}).count()+' time(s)');
resolve('No need to createa a stream, already exists'+Streams.find({name:customer.name}).count()+' time(s)');
}
})
.then(function(resultOfStreamStep){ //Copy the APP
console.log('2 COPY the app: result Of create Stream Step: ', resultOfStreamStep);
return copyApp(templateApp.guid, customer.name+' - ' + templateApp.name)
})
.then(function(appGuid){ //Publish into streamId
console.log('3 PUBLISH: APP HAS BEEN COPIED AND HAS RECEIVED GUID', appGuid);
return publishApp(appGuid, templateApp.name, streamId) //return publishApp(appGuid, templateApp.name+' - ' +customer.name , streamId)
})
.then(function(){
console.log('############## FINISHED CREATING THE TEMPLATE '+templateApp.name+' FOR THIS CUSTOMER: '+customer.name);
console.log(' ');
})
.catch(function(err) {
console.error(err);
// throw new Meteor.Error('Catch error app generation chain: App generation failed', 'err');
}).done();
return myPromise;
})) //each template Promise.all
}) //each customer
) //promise all generation total
};
function copyApp (guid, name) {
console.log('Copy template: '+guid+' to new app: '+name);
check(guid, String);
check(name, String);
return new Promise(function(resolve, reject){
HTTP.call( 'post', 'http://'+config.host+'/'+config.virtualProxy+'/qrs/app/'+guid+'/copy?name='+name+'&xrfkey='+config.xrfkey,
{
headers: {
'hdr-usr' : config.headerValue,
'X-Qlik-xrfkey': config.xrfkey
}
}, function( error, response ) {
if ( error ) {
console.error('error app copy', error );
throw new Meteor.Error('error app copy', error)
reject(error);
} else if (response){
// console.log('Copy app: HTTP request gave response', response.data );
console.log('Copy app: the generated guid: ', response.data.id);
resolve(response.data.id); //return app Guid
}
});
})
};
function publishApp (appGuid, appName, streamId) {
console.log('Publish app: '+appGuid+' to stream: '+streamId);
check(appGuid, String);
check(appName, String);
check(streamId, String);
return new Promise(function(resolve, reject){
HTTP.call( 'put', 'http://'+config.host+'/'+config.virtualProxy+'/qrs/app/'+appGuid+'/publish?name='+appName+'&stream='+streamId+'&xrfkey='+config.xrfkey,
{
headers: {
'hdr-usr' : config.headerValue,
'X-Qlik-xrfkey': config.xrfkey
}
}, function( error, response ) {
if ( error ) {
console.error('error publishApp', error );
throw new Meteor.Error('error publish App', error)
reject(error);
} else {
// console.log( response );
resolve('publishApp success');
}
});
})
};
function deleteApp (guid) {
return HTTP.call( 'DELETE', 'http://'+config.host+'/'+config.virtualProxy+'/qrs/app/'+guid+'?xrfkey='+config.xrfkey,
{
headers: {
'hdr-usr' : config.headerValue,
'X-Qlik-xrfkey': config.xrfkey
}
}, function( error, response ) {
if ( error ) {
console.error( error );
throw new Meteor.Error('error app delete', error)
} else {
console.log( response );
return response;
}
});
};
function deleteStream (guid) {
return HTTP.call( 'DELETE', 'http://'+config.host+'/'+config.virtualProxy+'/qrs/stream/'+guid+'?xrfkey='+config.xrfkey,
{
headers: {
'hdr-usr' : config.headerValue,
'X-Qlik-xrfkey': config.xrfkey
}
}, function( error, response ) {
if ( error ) {
console.error( error );
throw new Meteor.Error('error stream delete', error)
} else {
console.log( response );
return response;
}
});
};
function createStream (name) {
console.log('create the stream with name', name);
var myPromise = qrs.post('/qrs/stream', null, {"name": name})
.catch(err => {
console.log(err);
throw new Meteor.Error(err)
})
return myPromise
};
function getStreams () {
return qrs.get('/qrs/stream/full');
};
Meteor.methods({
generateStreamAndApp (customers) {
check(customers, Array);
generateStreamAndApp(customers)
.then(
function fulfilled (result) {
console.log('generation promise fulfilled, result of promise', result);
// resolve('Generation success ');
return 'Generation success';
},
function Rejected (error){
console.error('Promise Rejected: Error when trying generate the apps', error);
throw new Meteor.Error('Generation failed','Promise Rejected: Error when trying to generate the apps');
})
},
getApps () {
return new Promise((resolve, reject) => {
qsocks.Connect(engineConfig)
.then(function(global) {
global.getDocList().then(function(docList) {
resolve(docList) ;
});
})
})
.catch(err => {
throw new Meteor.Error(err)
})
},
copyApp (guid, name) {
check(guid, String);
check(name, String);
return copyApp(guid,name);
},
deleteApp (guid) {
check(guid, String);
return deleteApp(guid);
},
deleteStream (guid) {
check(guid, String);
return deleteStream(guid);
},
countApps(){
return qrs.get('/qrs/app/count');
},
countStreams(){
return qrs.get('/qrs/stream/count');
},
createStream (name) {
return createStream(name);
},
getStreams () {
return getStreams();
},
getSecurityRules () {
return getSecurityRules();
},
// updateAppsCollection(){
// console.log('Method: update the local mongoDB with fresh data from Qlik Sense');
// try{
// Apps.remove();
// }
// catch(error){console.log(err)};
// var myPromise = qrs.get( '/qrs/app/full');
// myPromise.then(
// function(docList){
// try{
// console.log('try to insert document array into mongo', docList);
// Apps.insert(docList,
// function(err,res){
// console.log("insert error: ",err);
// console.log("insert result: ",res);
// }
// );
// console.log('inserted document ', docList);
// }
// catch(error){console.log(err)}
// }, function (error) {
// console.error('uh oh: ', error); // 'uh oh: something bad happened’
// });
// myPromise.catch(function(error) {
// console.log('Caught!', error);
// });
// }
});
Meteor.startup(() => {
qrs = new QRS(config);
// console.log('Meteor server has started. Calling updateAppsCollection');
// Meteor.call('updateAppsCollection');
});
//CODE WITH NPM QRS, THAT GENERATES DOUBLE APPS
// function copyApp (guid, name) {
// console.log('Copy template: '+guid+' to new app: '+name);
// check(guid, String);
// check(name, String);
// return qrs.post('/qrs/app/'+guid+'/copy', [{"key": "name", "value": name}])
// .then(
// function fulfilled (result) {
// console.log('result of copy app promise', result);
// return result;
// },
// function Rejected (error){
// console.error('Promise Rejected: Error when trying to copy the app', error);
// throw new Meteor.Error('App copy failed', 'App copy failed');
// })
// };
// return
// qsocks.Connect(engineConfig)
// .then(function(global) {
// console.log(global);
// return global.getDocList()
// }).then(function(docList){
// console.log('DE DOC LIST IS: ', docList);
// return docList;
// })
Thank you!