0

I had an issue with my find() method.

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return await this.repository.findOne(id);
  }

Then I found out that I have to add where condition or replace it with findOneBy():

return await this.repository.findOne({ where: { id } });

or

return await this.repository.findOneBy({ id });

However, I still got the same error and I thought that it didn't work:

Error: You must provide selection conditions in order to find a single row.

Then I stopped start:dev script and ran npm run start:dev again. After that the error disappeared. Shouldn't this mode automatically reload everything when I make code changes or why do I have to stop and restart the server for the error to disappear?

I am using nestjs version: 9.2.0 with Ubuntu 20.04. Data is loaded from a MySQL DB via Docker.

Here are my dependencies from package.json:

  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
"dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "^9.0.0",
    "@nestjs/mapped-types": "^1.2.2",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/typeorm": "^9.0.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "mysql": "^2.18.1",
    "pg": "^8.10.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.12"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },

Update: The problem exists with other changes as well. I have changed more code and NestJS does not notice it.

I have added an Expection and had to restart the server to see the changes:

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    const event = await this.repository.findOneBy({ id });

    if (!event) {
      throw new NotFoundException();
    }

    return event;
  }

Update 2: "npm list" output:

├── @nestjs/cli@9.3.0
├── @nestjs/common@9.3.9
├── @nestjs/config@2.3.1
├── @nestjs/core@9.3.9
├── @nestjs/mapped-types@1.2.2
├── @nestjs/platform-express@9.3.9
├── @nestjs/schematics@9.0.4
├── @nestjs/testing@9.3.9
├── @nestjs/typeorm@9.0.1
├── @types/express@4.17.17
├── @types/jest@29.2.4
├── @types/node@18.11.18
├── @types/supertest@2.0.12
├── @typescript-eslint/eslint-plugin@5.54.1
├── @typescript-eslint/parser@5.54.1
├── class-transformer@0.5.1
├── class-validator@0.14.0
├── eslint-config-prettier@8.7.0
├── eslint-plugin-prettier@4.2.1
├── eslint@8.36.0
├── jest@29.3.1
├── mysql@2.18.1
├── pg@8.10.0
├── prettier@2.8.4
├── reflect-metadata@0.1.13
├── rxjs@7.8.0
├── source-map-support@0.5.21
├── supertest@6.3.3
├── ts-jest@29.0.3
├── ts-loader@9.4.2
├── ts-node@10.9.1
├── tsconfig-paths@4.1.1
├── typeorm@0.3.12
└── typescript@4.9.5

PS: Deleting the dist folder did not help.

r00k13
  • 215
  • 3
  • 15

1 Answers1

0

Adding "deleteOutDir": true under "compilerOptions" in your nest-cli.json file might help on solve this. It instructs the CLI to run rimraf before compiling and starting the app again.

Micael Levi
  • 5,054
  • 2
  • 16
  • 27
  • I already had this option `"deleteOutDir": true` in the file. This was apparently set automatically, since I did not adjust anything here myself. Thanks for the info though. Had not known that. Maybe this issue somehow related to my Ubuntu 20.04. or Docker (MySQL database). PS: The problem exists with other changes as well. Have changed other things and NestJS does not notice it without a manual restart. – r00k13 Mar 19 '23 at 18:34
  • Can I somehow force server restart in VSC or NestJS when I make changes to a file in the project (any files)? – r00k13 Mar 19 '23 at 18:53
  • I don't think so. – Micael Levi Mar 20 '23 at 00:18
  • 1
    Your link with `watchOptions` in the `tsconfig.json` solved my issue: https://docs.nestjs.com/faq/common-errors#file-change-detected-loops-endlessly . – r00k13 Mar 24 '23 at 17:09