Sails startDownload not working - TypeError: sails.startDownload is not a function - node.js

I am following https://sailsjs.com/documentation/reference/response-res/res-attachment
My code looks like this:
let file = require('path').resolve(document.path)
if (fs.existsSync(file)) {
this.res.attachment(document.name)
let downloading = await sails.startDownload(document.fsPath)
return exits.success(downloading)
}
Exits:
exits: {
success: {
statusCode: 200,
description: 'Document has been sent for download.'
},
}
I am getting error
TypeError: sails.startDownload is not a function
Sails version 1.1.0

Have you installed sails-hook-uploads as mentioned in the documentation you have linked to?

Related

Cannot read property 'filename' of undefined Using html-pdf with lambda

I'm experiencing problems when I try to create a PDF from HTML using Lambda Function, I'm receiving the error below:
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'filename' of undefined",
"trace": [
"TypeError: Cannot read property 'filename' of undefined",
" at execPdfToBuffer (/var/task/node_modules/html-pdf/lib/pdf.js:48:21)",
" at ChildProcess.respond (/var/task/node_modules/html-pdf/lib/pdf.js:144:5)",
" at ChildProcess.emit (events.js:314:20)",
" at ChildProcess.EventEmitter.emit (domain.js:483:12)",
" at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)"
]
}
My code is:
try{
let arq = await new Promise((res,rej)=>{
pdf.create(html,{
format: "Letter",
orientation: "portrait",
phantomPath: '/opt/phantomjs_linux-x86_64'
}).toBuffer(function(err, buffer){
if (err){
rej(false);
}else{
res(buffer);
}
});
});
const params = {
Key: 'teste.pdf',
Body: arq, // <---------
Bucket: 'temp'
};
let S3 = new AWS.S3();
let response = await S3.upload(params).promise();
if (response){
return true;
}else{
return false;
}
}catch(err){
console.log(err);
return false;
}
I've read this topic: html-pdf package is not working on aws lambda
But the proposed solution didn't work for me.
Thank you in advance
In the version 3.0.1 you can set the flag localUrlAccess: true, , it will work
I ran into this issue, too. I fixed it by downgrading html-pdf from version 3.0.1 to version 2.2.0.
The release notes for the newer version are less than inspiring: "Not sure this module is even usable without installing phantomjs manually"
Perhaps there's a way to make version 3 work with lambda. But downgrading was a quick win for me.

Cypress for Electron - Using FS

I am using Cypress to test my Electron application.
Since Cypress uses the browser mode, FS is not supported.
So I am getting this error:
Error in mounted hook: "TypeError: fs.existsSync is not a function"
And I found this on the documentation:
https://docs.cypress.io/api/commands/task.html#Event
So I added this on my test:
it('Sample test', () => {
cy.task('readSettingsJson', settingsFolder).then((content) => {
// This can print the JSON file contents correctly
console.log('content = ' + content)
})
})
And on my plugins/index.js:
on('task', {
readSettingsJson(foldername) {
if (!fs.existsSync(foldername)) {
fs.mkdirSync(foldername, { recursive: true })
// some command to copy the file
} else {
// This is what I am testing at this moment
return fs.readFileSync(path.join(filename, '/settings.json'), 'utf8')
}
return null
}
})
However, it doesnt seem to work. I still get the error:
Error in mounted hook: "TypeError: fs.existsSync is not a function"
And despite the test printing the json file correctly, my app still can't load the JSON file.
Am I missing anything? Help please!
Cypress support for Electron apps are in very early alpha release:
https://www.cypress.io/blog/2019/09/26/testing-electron-js-applications-using-cypress-alpha-release/
As an alternative, try using Spectron, which is the testing framework that is currently recommended by Electron team:
https://www.electronjs.org/spectron

JHipster generator: addMavenDependency is not defined

I'm trying to create a JHipster generator to setup Axon2 for the generated project.
In order to add a java library to the project I'using the function
addMavenDependency in the index.js,
try {
addMavenDependency('org.axonframework', 'axon-integration', '2.4.6','');
}catch (e) {
but I receive the following error:
ERROR!
Problem when adding the new libraries in your pom.xml
You need to add manually:
"org.axonframework:axon-integration": "2.4.6",
ReferenceError: addMavenDependency is not defined
Any help will be really appreciated.
You need to extend the BaseGenerator and call this.addMavenDependency().
Unless you are composing with another generator, then you can pass an object to be populated with the variables and functions being used by the generator like so:
const jhipsterVar = { moduleName: 'your-module' };
const jhipsterFunc = {};
module.exports = generator.extend({
initializing: {
compose() {
this.composeWith('other-module',
{ jhipsterVar, jhipsterFunc },
this.options.testmode ? { local: require.resolve('generator-jhipster/generators/modules') } : null
);
}
},
writing: {
jhipsterFunc.addMavenDependency('com.test', 'test', '1.0.0');
}
});

yargs .check() error handling

I'm using yargs to validate cli arguments for a data-loading helper lib.
I want to be able to check that a file exists before allowing the script to run, which I do with fs.accessSync(filename, fs.R_OK);. However, if the file does not exist, the messaging simply shows the .check() function as the error, whereas I want to intercept, and state that the file does not exist (with read permissions).
So how to I send an error to be presented by .check() on a false return?
Here is the gist of my yargs:
var path = {
name: 'filepath',
options: {
alias: 'f',
describe: 'provide json array file',
demand: true,
},
};
function fileExists(filename) {
try {
fs.accessSync(filename, fs.R_OK);
return true;
} catch (e) {
return false;
}
}
var argv = require('yargs')
.usage('$0 [args]')
.option(path.name, path.options)
.check(function (argv) {
return fileExists(argv.f);
})
.strict()
.help('help')
.argv;
and the returned error if not a readable file:
Argument check failed: function (argv) {
return fileExists(argv.f);
}
I'd prefer to be able to specify something along the lines of:
Argument check failed: filepath is not a readable file
So in yargs 5.0.0 when you return a non-truthy value it will print that entire output.
Argument check failed: function (argv) {
return fileExists(argv.f);
}
If you throw instead you can control the output message.
.check((argv) => {
if (fileExists(argv.f)) {
return true;
}
throw new Error('Argument check failed: filepath is not a readable file');
})

Uncaught Error: [$injector:unpr] Unknown provider: sessionInjectorProvider

I am new to angular js, loopback and nodejs,
While implementing the authentication in the angular app, I am getting the below mentioned error
Uncaught Error: [$injector:unpr] Unknown provider: sessionInjectorProvider <- sessionInjector <- $http <- $compile
I was going through this document, but no help.
http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/
This error came when I added the below lines for sessionInjector
angular.module('myApp', []).factory('sessionInjectorProvider', ['SessionService', function(SessionService) {
var sessionInjector = {
request: function(config) {
if (!SessionService.isAnonymus) {
config.headers['x-session-token'] = SessionService.token;
}
return config;
}
};
return sessionInjector;
}]);
angular.module('myApp', []).config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('sessionInjector');
}]);
There are for sure at least two errors in your code:
angular.module('myApp', []) creates a module, whereas angular.module('myApp') calls the module. This means that at the end of your code you're creating again the module and hence losing what you had written before.
There are different ways to format this code, one that would solve the problem would be:
angular.module('myApp', [])
.factory('sessionInjectorProvider', ['SessionService', function(SessionService) {
var sessionInjector = {
request: function(config) {
if (!SessionService.isAnonymus) {
config.headers['x-session-token'] = SessionService.token;
}
return config;
}
};
return sessionInjector;
}])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('sessionInjectorProvider');
}]);
Also, as mentioned already, you're mixing 'sessionInjectorProvider' and 'sessionInjector' - your interceptor should use 'sessionInjectorProvider' as shown in the code I posted above.

Resources