Currently I have the following code:
import { Injectable, Logger } from '#nestjs/common';
#Injectable()
export class CategoriesService {
logger: Logger;
constructor(#InjectModel(Categories.name) private categoriesModel: Model<CategoriesDocument>) {
this.logger = new Logger();
}
...
In a method I then attempt to log:
getCategories({ text, first, page, hasType, parent }: GetCategoriesArgs) {
this.logger.log('getCategories is triggered');
...
I don't get an error but I don't see the log message in terminal window. Any ideas what I'm missing? Also console.log() also doesn't appear.
I had the same issue - strangely, it appeared to be a cacheing issue. In my case I had to:
delete the /dist folder and
re-run npm run start:dev
Both console.log(...) and this.logger.log(...) instance I setup began working.
Related
I'm pretty new to node development and have the issue mentioned in the headline. I figured out, that this is probably a circular dependency issue. But I really don't get why.
Maybe you can help?
Here is the first part of my application that works.
I have a simple abstract class like this:
export abstract class AbstractApplication {
abstract beforeStart(): void
abstract afterStart(): void
}
Then I have a second class that extends from that AbstractApplication:
export class App extends AbstractApplication {
//implement the specific methods from AbstractApplication here and do things
async startApp(): void {
//do things here
}
}
Inside this same file, I create an instance of the App class and export it into the module like this:
const app: App = new App()
module.exports = app
Then, inside another file, the server.js, I require this app and start it like this:
const app = require("./src/App")
app.startApp()
And if I run npm run start which calls node server.js everything works and the App runs.
Now I have a seperate file that contains a third class that extends from the App class like this:
export class TestSuite extends App {
async startApp(): Promise<void> {
console.log("READY")
}
}
So as you can see, I take the app class but I override the startApp function.
Inside this same file I do the same thing I did in the App file before:
const testSuite = new TestSuite()
module.exports = testSuite
Then in a last file, I'm doing this:
const testSuite = require("./TestSuite")
testSuite.startApp()
If I now change my npm start to node theOtherFileWithTestSuiteStart.js and run npm run start, everything works also fine.
Now, the last file which contains the testSuite.startApp()should contain test functions that depend on that application, just something simple as
const testSuite = require("./TestSuite")
testSuite.startApp()
test('requests the "/" route', function () {
//test something here
})
And when I run this with tap, then I get the error above: TypeError: Class extends value undefined is not a constructor or null
The interesting thing is: If I extend the TestSuite class from AbstractApplication, I at least get an error that the test is unfinished (which may be okay because of dummy implementation). If I extend it from the App class, this error occures.
Any ideas?
We're building a NodeJS Framework MidwayJS, and we recommend our users to use Jest to make test cases. We modified Jest Env like this:
'use strict';
const NodeEnvironment = require('jest-environment-node');
/* eslint-disable no-useless-constructor */
class JestEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
require('ts-node/register');
this.global.process.env.MIDWAY_TS_MODE = 'true';
this.global.process.env.MIDWAY_JEST_MODE = 'true';
this.global.setTimeout(3000)
await super.setup();
}
async teardown() {
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = JestEnvironment;
With configuration above it goes well, but we got a problem:
Some users create huge project directory and our framework will scan the whole directory before application start, when running test cases it acts also in this way, which makes it in some suitations take more than 3000ms--Jest set default async callback called time to be 3000ms, so if the scanning didn't finished in 3000ms the test would crash.
we solved this easily by use jest.setup.js in one line : jest.setTimeout(30000), but now we want to solve it in the file which showed in the first, and I didn't find a way to modify setup config in it.
I'd appreciate it greatly if you can help me to make it out.
I'm using NestJs to create a couple of applications and I want to move the code from a NestInterceptor for an external NPM Package so I can use the same interceptor in multiple applications.
The problem is that the same code that works when used "locally" just stop working when moved to the external package.
Here's the code for the interceptor:
import { Injectable, NestInterceptor, CallHandler, ExecutionContext } from '#nestjs/common'
import { map } from 'rxjs/operators'
import { getManager } from 'typeorm'
import jwt_decode from 'jwt-decode'
#Injectable()
export class MyInterceptor implements NestInterceptor {
entity: any
constructor(entity: any) {
this.entity = entity
}
async intercept(context: ExecutionContext, next: CallHandler): Promise<any> {
const request = context.switchToHttp().getRequest()
const repository = getManager().getRepository(this.entity)
return next.handle().pipe(map((data) => data))
}
}
Here's a given controller:
import { myInterceptor } from "../src/interceptors/interceptor.ts";
#UseInterceptors(new CompanyIdInterceptor(User))
export class UserController {
}
This works fine, but if a move the file to an external NPM package and import from it like this:
import { myInterceptor } from "mynpmpackage";
I get the following error:
[Nest] 24065 - 04/18/2019, 10:04 AM [ExceptionsHandler] Connection "default" was not found. +26114ms
ConnectionNotFoundError: Connection "default" was not found.
at new ConnectionNotFoundError (/home/andre/Services/npm-sdk/src/error/ConnectionNotFoundError.ts:8:9)
at ConnectionManager.get (/home/andre/Services/npm-sdk/src/connection/ConnectionManager.ts:40:19)
Any ideas, on what causes this and how to solve it?
This might not be your problem exactly, but I had a similar problem when moving things to external packages with TypeORM. Make sure all packages from parent project are using the same version of the TypeORM package.
In my case, using yarn why typeorm showed me two different versions were being installed. One of them was used to register the entities, while the framework connected to the SQL database using another version, generating this clash.
Check your versions using yarn why [pkg-name] or if you're using NPM, try npx npm-why [pkg-name] or install globally from https://www.npmjs.com/package/npm-why.
After verifying TypeOrm versions is same in both the packages i.e- external package and consumer repository as mentioned by #Luís Brito still issue persist then issue could be-
Basically when we create an external package - TypeORM tries to get the "default" connection option, but If not found then throws an error:
ConnectionNotFoundError: Connection "default" was not found.
We can solve this issue by doing some kind of sanity check before establishing a connection - luckily we have .has() method on getConnectionManager().
import { Connection, getConnectionManager, getConnectionOptions,
createConnection, getConnection, QueryRunner } from 'typeorm';
async init() {
let connection: Connection;
let queryRunner: QueryRunner;
if (!getConnectionManager().has('default')) {
const connectionOptions = await getConnectionOptions();
connection = await createConnection(connectionOptions);
} else {
connection = getConnection();
}
queryRunner = connection.createQueryRunner();
}
Above is a quick code-snippet which was the actual root cause for this issue but If you are interested to see complete working repositories (different example) -
External NPM Package :
Git Repo : git-unit-of-work (specific file- src/providers/typeorm/typeorm-uow.ts)
Published in NPM : npm-unit-of-work
Consumer of above package : nest-typeorm-postgre (specific files- package.json, src/countries/countries.service.ts & countries.module.ts)
I try to use Electron and Angular5 to write my first desktop App but unfortunately i am stuck in using the fs module. It seems that I have imported fs correctly (no errors within Visual Studio Code and code completion) but when i tried using "fs.readFile" the console prints out this error:
Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_2_fs__.readFile is not a function
This is the code of my service so far:
import { Injectable } from '#angular/core';
import { ElectronService } from 'ngx-electron';
import * as fs from 'fs';
import { OpenDialogOptions } from 'electron';
#Injectable()
export class FileService {
dialog = this._electronService.remote.dialog;
window = this._electronService.remote.getCurrentWindow();
constructor(private _electronService: ElectronService) { }
loadFileContent(): void{
this.dialog.showOpenDialog(this.window, {},(fileNames) => {
if(fileNames === undefined){
console.error("no files selected!");
return;
}
fs.readFile(fileNames[0], "utf-8", (err, data) => {
if(err){
console.error("Cannot read file ",err);
return;
}
console.log("The content of the file is : ");
console.log(data);
});
});
}
}
Do I miss something here? Seems that fs is not loaded or something? Thanks for your help everyone!
You can also use remote.require to load native node modules from ngx-electron.
fs;
constructor(private _electronService: ElectronService) {
this.fs = this._electronService.remote.require('fs');
}
I found the answer with the help of the comments from kimy82!
First i needed to get the Angular5 webpack.config.js by simply using:
ng eject
After that i opened up the webpack.config.js and added the following:
"target": "node-webkit"
Simply "node" did not work out for me and since electron uses a Chromium this should be ok.
Thanks everyone!
Your browser cannot access the file system on the server. fs should not be loaded in the browser
child.js
class Child {
constructor(){
this.helloWorld = "Hello World";
}
run() {
}
}
export default new Child();
parent.js
import child from './child.js';
class Parent {
constructor() {
this.child = child;
}
}
export default new Parent();
index.js
import parent from './parent.js'
console.log(parent.child.helloWorld); <-- does not throws an error, displays "Hello World"
console.log(parent.child.run); <-- throws an error (Cannot read property run from undefined)
console.log(parent.child.run()); <-- throws an error (Cannot read property run from undefined)
If I do console.log(parent.child) in index.js, run does not show up, however the property helloWorld does..
How can I have the functions exposed as well? I was hoping to be able to do this to help keep my code a bit more organized, so was going to separate it out into separate classes to help minimize the amount of code in each file.
To make one thing clear from the start: The error you seem to get has nothing to do with run not appearing in the console.log output.
If your code really throws that error then that means that the value of parent.child is undefined. Hence when you call console.log(parent.child), you should see undefined, not an object. However, I don't see why you'd get that error.
Anyways, run is defined on the prototype of parent.child, not on itself. console.log most likely shows an object's own properties (the console API is not standardized, so results can vary between environments). That's normal.
Simple example to reproduce:
var foo = {
x: 42
};
var bar = Object.create(foo);
bar.y = 21;
console.log(bar, bar.x, bar.y);
// Open the browser console to see output
bar.x is accessible even though console.log doesn't show it (in Chrome at least).
Well I'm not sure if helps you to solve the problem, but whenever I want to add inheritance, I use extends and super here is an example:
Base Class:
class BaseDataModel {
constructor() {
}
getModel() {
return 'model';
}
module.exports.BaseDataModel = BaseDataModel;
Class extending Base Class:
"use strict"
// Imports
const BaseDataModel = require('../baseDataModel').BaseDataModel; // use the proper location
class UserMembershipModel extends BaseDataModel {
constructor() {
super(); // this is optional, I use this to inherit the constructors
}
getChildModel() {
return super.getModel(); // This is how you access the function from your extended class
}
module.exports.UserMembershipModel = UserMembershipModel;
Again, not sure if it solves your problem, since your actually adding a property with a Child class. My example is actually extending (or UserMembershipModel inherits from BaseDataModel).
Hope this helps you a bit.