gpt4 book ai didi

javascript - TypeScript 引用错误 : is not defined

转载 作者:行者123 更新时间:2023-11-30 11:30:20 24 4
gpt4 key购买 nike

我有一个 typescript 应用程序。它看起来像这样:

沙龙.ts

export class Salon {
clients: Client [];
constructor() {
this.clients = [];
}

public addClient(c: Client) {
this.clients.push(c);
}}

客户端.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未定义”。我该如何处理这个问题?

最佳答案

这是因为在 var/const/let 的帮助下错过了变量声明。您只需在 s 之前添加 let 即可指定您正在创建新变量,但不使用旧变量。

关于javascript - TypeScript 引用错误 : is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46477130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com