简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM