ngrx-course
ngrx-course copied to clipboard
running npm server produces error
c:\git-repositories\github\ngrx-course>npm run server
[email protected] server c:\git-repositories\github\ngrx-course ts-node -P ./server/server.tsconfig.json ./server/server.ts
c:\git-repositories\github\ngrx-course\node_modules\ts-node\src\index.ts:307
throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
^
TSError: ⨯ Unable to compile TypeScript
server\search-lessons.route.ts (19,35): Argument of type 'string | ParsedQs | string[] | ParsedQs[]' is not assignable to parameter of type 'string'.
Type 'ParsedQs' is not assignable to type 'string'. (2345)
server\search-lessons.route.ts (20,33): Argument of type 'string | ParsedQs | string[] | ParsedQs[]' is not assignable to parameter of type 'string'.
Type 'ParsedQs' is not assignable to type 'string'. (2345)
server\search-lessons.route.ts (22,63): This condition will always return 'false' since the types 'number' and 'string | ParsedQs | string[] | ParsedQs[]' have no overlap. (2367)
server\search-lessons.route.ts (25,102): Property 'toLowerCase' does not exist on type 'string | ParsedQs | string[] | ParsedQs[]'.
Property 'toLowerCase' does not exist on type 'string[]'. (2339)
at getOutput (c:\git-repositories\github\ngrx-course\node_modules\ts-node\src\index.ts:307:15)
at c:\git-repositories\github\ngrx-course\node_modules\ts-node\src\index.ts:336:16
at Object.compile (c:\git-repositories\github\ngrx-course\node_modules\ts-node\src\index.ts:496:11)
at Module.m._compile (c:\git-repositories\github\ngrx-course\node_modules\ts-node\src\index.ts:392:43)
at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Object.require.extensions.ts-node -P ./server/server.tsconfig.json ./server/server.ts
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] server script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\DHarms\AppData\Roaming\npm-cache_logs\2020-07-30T11_26_50_778Z-debug.log
c:\git-repositories\github\ngrx-course>
this is a windows pc with the latest verison of Node
this is in the 1-start branch
solved it by adding toString in three places
import {Request, Response} from 'express';
import {LESSONS} from "./db-data";
import {setTimeout} from "timers";
export function searchLessons(req: Request, res: Response) {
console.log('Searching for lessons ...');
const queryParams = req.query;
const courseId = queryParams.courseId,
filter = queryParams.filter || '',
sortOrder = queryParams.sortOrder || 'asc',
pageNumber = parseInt(queryParams.pageNumber.toString()) || 0, // added toString() here
pageSize = parseInt(queryParams.pageSize.toString()); // added toString() here
let lessons = Object.values(LESSONS).filter(lesson => lesson.courseId === +courseId).sort((l1, l2) => l1.id - l2.id);
if (filter) {
lessons = lessons.filter(lesson => lesson.description.trim().toLowerCase().search(filter.toString().toLowerCase()) >= 0); // added toString() here
}
if (sortOrder === 'desc') {
lessons = lessons.reverse();
}
const initialPos = pageNumber * pageSize;
console.log(`Retrieving lessons page starting at position ${initialPos}, page size ${pageSize} for course ${courseId}`);
const lessonsPage = lessons.slice(initialPos, initialPos + pageSize);
res.status(200).json(lessonsPage);
}
Also you have transformed string to number (+courseId) in line 25. I've encountered the same issue. Thanks!