2017-09-28 165 views
0

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

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我想要得到與客戶端的信息後請求並將其添加到客戶端陣列:

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未定義」。 我該如何處理?

回答

1

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