簡體   English   中英

TypeScript ReferenceError:未定義

[英]TypeScript ReferenceError: is not defined

我有一個打字稿應用程序。 它看起來像這樣:

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;
}}

在我的服務器文件Serv.ts中,我希望獲得帶有客戶端信息的發布請求並將其添加到clients數組:

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));
}
});

當我運行我的應用程序並嘗試發布請求時,它會給我一個錯誤“ReferenceError:s未定義”。 我該如何處理?

這是因為在var/const/let幫助下錯過了變量聲明。 您應該在s之前添加let以指定您正在創建新變量,但不使用舊變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM