2017-09-28 35 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以指定您正在创建新变量,但不使用旧变量。