Volcara Ebani
Volcara Ebani

Reputation: 21

TypeScript ReferenceError: is not defined

I have a typescript app. It looks like this:

Salon.ts

export class Salon {
clients: Client [];
constructor() {
    this.clients = [];
}

public addClient(c: Client) {
    this.clients.push(c);
}}

Client.ts

class Client {
name: string;
surname: string;
constructor(name, surname){
    this.name = name;
    this.surname = surname;
}}

And in my server file Serv.ts I want to get post requests with client information and add it to the clients array:

import {Salon} from "./Salon.js";
s: Salon = new Salon();

app.post("/addClient", (req, res) => {
if (req.query.name === undefined | req.query.surname=== undefined){
    res.status(400);
    res.setHeader("Content-Type", "text/html; charset=utf-8");
    res.end("Your name and surname please");
} else {
    console.log(req.query.name, req.query.age);
    s.addClient(new Client(req.query.name, req.query.surname));
}
});

And when I run my app and trying to make post request it give me an error "ReferenceError: s is not defined". How do I deal with this?

Upvotes: 2

Views: 17965

Answers (1)

Sergey
Sergey

Reputation: 7692

That is because of missed variable declaration with help of var/const/let. You should just add let before the s to specify that you are creating the new variable, but no using the old one.

Upvotes: 6

Related Questions