jsreport 2.0 BeforeRender Not Working - handlebars.js

I am working on jsreport v2.0 and wants to render the data for the report. I am using handlebars and phantom-pdf and my beforeRender function is not getting called by default.
For jsreport v2.0, i have added the listener for the beforeRender as following but still it did not seem to be called by default to render the data.
function beforeRenderListeners1(req,res){
console.log("Listener Called");
}
const jsreport = require('jsreport-core')({
})
jsreport.beforeRenderListeners.add('beforeRenderListeners1', (req, res) => {
console.log("hello");
req.data.check = abc();
})

since i don't know the complete code that you are using i will go ahead and provide you a snippet that works in latest jsreport-core v2 (2.0.3) with node 8
const jsreport = require('jsreport-core')()
jsreport.use(require('jsreport-handlebars')())
jsreport.beforeRenderListeners.add('beforeRenderListeners1', (req, res) => {
console.log("before render called")
req.data = req.data || {}
req.data.check = 'check pass'
})
jsreport.init().then(() => {
console.log('started')
return jsreport.render({
template: {
content: '<p>sample demo content, check: {{check}}</p>',
engine: 'handlebars',
recipe: 'html'
}
})
}).then((res) => {
console.log('render done')
console.log(res.content.toString())
}).catch((err) => console.error(err))
put that in a file and then run it, you will see the message before render called being printed in console.

Related

Jupyterlab load model fromJSON issue

I am developing notebook syncronization mechanism using websockets between jupyterlab users editing same notebook, here is what i am doing:
// having notebook instance as a NotebookPanel
// I am merging UI cell data and server (other's edited) cell data
// `updatedModel` contains merged cells from server and from user's current changes in the notebook.
notebook.context.model.fromJSON(updatedModel.content);
I am facing following issue:
and cell actions disappear.
did anyone have this issue and came up with a solution to properly load notebook from a json?
Jupyterlab Version 3.5.0
Reproduction Code:
import {JupyterFrontEnd, JupyterFrontEndPlugin} from "#jupyterlab/application";
import {INotebookTracker, NotebookPanel, NotebookTracker} from "#jupyterlab/notebook";
const testCopyModelFromJSON = (notebookTracker: NotebookTracker) => {
notebookTracker.widgetAdded.connect((tracker: NotebookTracker, panel: NotebookPanel) => {
let notebook = panel.content;
const notebookModel = notebook.model;
if (!notebookModel) {
return;
}
const model = notebookModel.toJSON();
notebookModel.fromJSON(model);
});
}
const plugin: JupyterFrontEndPlugin<void> = {
id: 'nbsync-extension:plugin',
autoStart: true,
requires: [INotebookTracker],
activate: (app: JupyterFrontEnd, notebookTracker: NotebookTracker) => {
console.info('nbsync-extension started');
app.started
.then(() => {
testCopyModelFromJSON(notebookTracker);
});
app.restored
.then(() => {
testCopyModelFromJSON(notebookTracker);
});
}
};
export default plugin;
dependencies:
"#jupyterlab/application": "^3.5.0",
"#jupyterlab/notebook": "^3.5.0"

Ionic rxjs pipe function not getting called after http post

I am using ionic 5
when I try to connect to firebase using post and get the response data and process the response using pipe and tap then it's not working. The log is not printing.
But when I replace the pipe with subscribe then it's working correctly and I can see the log correctly.
Please see the working and not working code below.
Can someone please help me to solve this issue. Thanks for your help.
WORKING CODE
return this.http.post("https://project-name.firebaseio.com/offered-places.json", {
...newPlace,
id: null
}).subscribe(resDate => {
console.log(resDate);
});
NOT WORKING CODE
return this.http.post("https://project-name.firebaseio.com/offered-places.json", {
...newPlace,
id: null
}).pipe(
tap(resData => {
console.log(resData);
})
);
As stated in comments you have to call subscribe method
Here's the snippet which reproduces your cases and provides mentioned solution.
const exampleObservable1 = rxjs.of([{}]);
const exampleObservable2 = rxjs.of([{}]);
const exampleObservable3 = rxjs.of([{}]);
console.log('working example');
exampleObservable1.subscribe(resDate => {
console.log(resDate);
});
console.log('not working example');
exampleObservable2.pipe(
rxjs.operators.tap(resData => {
console.log(resData);
}))
console.log('suggestion');
exampleObservable3.pipe(
rxjs.operators.tap(resData => {
console.log('tap', resData);
})).subscribe(resDate => {
console.log('subscription', resDate);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.js"></script>

How to load a file into an Angular Test?

i'm trying to test an extension validator using Karma and Jasmine with latest Angular version. I have a service that receives a File and checks its extension and MIME type, this is the method signature:
public validateFileType(file: File, validTypes: string[]): Observable<boolean>
As you can see the method receives a File, so i want to either load some files i have in a /testing folder or create a file with '.doc'/'.xls'/'.pdf' extension (i would like to use the first approach, already tried creating a .doc, .xls file and the signature numbers does not match any real file).
But its impossible to load any File as the HttpClient can not be instantiated in a Karma test, i have tried everything i know and searched a lot, any help is really appreciated.
Example
import { TestBed } from '#angular/core/testing';
import { FileValidatorService } from './file-validator.service';
describe('FileValidatorService', () => {
let service: FileValidatorService;
beforeEach(() => {
TestBed.configureTestingModule({ providers: [FileValidatorService] });
service = TestBed.get(FileValidatorService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
describe('Types', () => {
it('should be false', () => {
const content = 'Hello test';
const data = new Blob([content], { type: 'application/zip' });
const arrayOfBlob = new Array<Blob>();
arrayOfBlob.push(data);
const applicationZip = new File(arrayOfBlob, 'Mock.zip', { type: 'application/zip' });
service.validateFileType(applicationZip, ['.doc', '.xls']).subscribe((result: boolean) => {
expect(result).toBeFalsy();
});
});
});
});
This test only works because the file i created generates some random signature code that i don't use so its not recognized and it doesn't match the expected types so is not working as it should.

How to test the return value of a promise in a Cucumber test?

I'm trying to test the return value of a get request to a couchdb node.
I have a feature defined with the following Given clause:
Given A get request is made to the DB
which is implemented with the following step function:
var Profile = require('../UserProfile.js')
Given('A get request is made to the DB', function () {
var text = Profile.getDB('localhost:5984').then(data => {
console.log(data)
})
});
The above step references this model:
var axios = require('axios')
module.exports = {
getDB: function(url){
return axios.get(url).then(response => {
return response.data
})
}
};
I can't seem to log the result of the GET request when I perform it in the model and reference it in the step definition. When I do the GET request in the step definition, it works - but this isn't useful to me, I want to test the model. How do I get the resulting value?
Cucumber 2.0 supports Promises as returns, try with:
const Profile = require('../UserProfile')
const { defineSupportCode } = require('cucumber')
defineSupportCode(({ defineStep }) => {
defineStep('A get request is made to the DB', function () {
return Profile.getDB('http://localhost:5984').then(data => {
console.log(data)
})
})
})

angular2 How to store response header data from http.get in extra variable?

I'm quite new to Angular 2 and Typescript and want to build a Card-Search App.
Basically I have a component where the response data is stored and observed...
this.searchFormGroup.valueChanges
.debounceTime(200)
.distinctUntilChanged()
.switchMap(searchFormGroup => this.mtgService.getCardsByName(searchFormGroup))
.subscribe(
searchResult => {
this.searchResult = searchResult.cards;
},
err => {
console.log(err);
});
...and a service, which sends the http.get request to the API ...
getCardsByName(searchFormGroup){
this.params.set(...) ...
return this.http.get(this.nameUrl, { search: this.params })
.map(res => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'))}
... communicating with each other.
What's the best way to store header-data from the request inside the component? Basically I need the total card-count and paging-links of the request, which are available inside the response header. I've been trying to find a solution to this for hours :o
Ok try and error got me a solution to this problem:
component activated service's http.get -> service responded with res.json() -> component only got res in JSON, what seems to delete the response header.
My workaround is:
Component:
this.searchFormGroup.valueChanges
.debounceTime(200)
.distinctUntilChanged()
.switchMap(searchFormGroup => this.mtgService.getCardsByName(searchFormGroup))
.subscribe(
res => {
console.log(res.headers); //works fine now!
this.searchResult = res.json().cards;
},
err => {
console.log(err);
});
Service:
getCardsByName(searchFormGroup){
this.params.set(...) ...
return this.http.get(this.nameUrl, { search: this.params })
.catch((error:any) => Observable.throw(error.json().error || 'Server error')) }
So the full res gets passed back to the component now.
However: any tips to make this better?

Resources