我正在尝试使用 Typescript 加载 .env 环境变量。
这是我的.env和app.ts文件
//.env
DB_URL=mongodb://127.0.0.1:27017/test
Run Code Online (Sandbox Code Playgroud)
//.env
DB_URL=mongodb://127.0.0.1:27017/test
Run Code Online (Sandbox Code Playgroud)
当我app.ts使用ts-node src/app.ts命令运行时,抛出此错误
// app.ts
import * as dotenv from 'dotenv';
import express from 'express';
import mongoose from 'mongoose';
dotenv.config();
mongoose.connect(process.env.DB_URL, config);
Run Code Online (Sandbox Code Playgroud)
但是当我在下面添加 if 语句时,它运行良好
Unable to compile TypeScript:
src/app.ts:50:18 - error TS2769: No overload matches this call.
Overload 1 of 3, '(uris: string, callback: (err: MongoError) => void): Promise<typeof import("mongoose")>', gave the following error.
Argument of type 'string | undefined' is not …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 制作一个简单的应用程序nestjs。
当我添加时CreateUserDTO,UserService我收到以下 TS 错误。
src/user/user.service.ts:11:40 - 错误 TS2345:“CreateUserDTO”类型的参数不可分配给“User”类型的参数。类型“CreateUserDTO”缺少类型“User”中的以下属性:$add、$set、$get、$count 以及其他 32 个属性。11 返回await this.userModel.create(userData);
我查了很多关于DTO的代码,但不知道原因。
这是代码。
import { Column, Model, PrimaryKey, Table } from 'sequelize-typescript';
@Table
export class User extends Model<User> {
@PrimaryKey
@Column
userId: string;
@Column
name: string;
}
Run Code Online (Sandbox Code Playgroud)
export class CreateUserDTO {
userId: string;
name: string;
}
Run Code Online (Sandbox Code Playgroud)
@Injectable()
export class UserService {
constructor(@InjectModel(User) private userModel: typeof User) {}
async createUser(userData: CreateUserDTO) {
const user = await this.userModel.create<User>(userData);
}
}
Run Code Online (Sandbox Code Playgroud)