2

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?

0

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.