我将Typeorm与class-validator结合使用,所以我定义了一个像这样的实体:
import {
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
BeforeInsert,
BeforeUpdate,
getRepository
} from "typeorm";
import {
validateOrReject,
IsDefined,
} from "class-validator";
import errors from 'restify-errors';
@Entity()
export class License extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
public id!: string;
@Column({ nullable: false })
@IsDefined({ message: 'name field was not provided' })
public name!: string;
@Column({ nullable: false })
@IsDefined({ message: 'description field was not provided' })
public description!: string;
@BeforeInsert()
@BeforeUpdate()
async validate() {
await validateOrReject(this, { skipUndefinedProperties: true });
// …
Run Code Online (Sandbox Code Playgroud) 假设我有这个对象数组:
let arr =
[
{ id: "1"},
{ id: "2"},
{ id: "3"}
]
Run Code Online (Sandbox Code Playgroud)
我会创建一个数组列表,所以我试过:
arr.map(x => x.id);
Run Code Online (Sandbox Code Playgroud)
但这将返回:
["1", "2", "3"]
Run Code Online (Sandbox Code Playgroud)
我希望每个值都有一个数组,例如: ["1"] ["2"] ["3"]
几天前我开始学习 Node.js,我面临着一个关于数据库和Gmail连接凭据的问题(最后一个是 需要的nodemailer
)。
我基本上创建了一个这样的文件:
const config = {
development: {
url: '127.0.0.1',
database: {
host: 'mongodb://localhost',
port: '27017',
db: 'foo'
},
gmail: {
username: 'foo@gmail.com',
password: 'foo',
},
server: {
host: '127.0.0.1',
port: '3000'
}
},
production:{
url: 'https://my.site.com',
database: {
host: '127.0.0.1',
port: '27017',
db: 'foo'
},
gmail: {
username: 'foo@gmail.com',
password: 'foo',
},
server: {
host: '127.0.0.1',
port: '3000'
}
}
};
module.exports = config;
Run Code Online (Sandbox Code Playgroud)
我不喜欢在应用程序中输入这些信息,所以经过一些研究,我找到了这个库:dotenv。该库将信息存储在process.env属性内,但实质是相同的......为什么我应该更喜欢像dotenv
针对我的config
模块的解决方案? …
我正在尝试扩展asRequest
的接口Express
:
import express, { Request, Response } from 'express';
interface IRequest extends Request {
user: {
id: string;
}
}
const router = express.Router();
router.get('/', auth, async (req: IRequest, res: Response) => {
try {
const user = await User.findById(req.user.id).select('-password');
res.json(user);
} catch (e) {
console.error((e as Error).message);
res.status(500).send('Server Error');
}
});
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
没有重载与此调用匹配。重载第 1 个(共 3 个)“(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Router”,出现以下错误。类型“(req: IRequest, res: Response) => Promise”的参数不可分配给类型“RequestHandler<ParamsDictionary, any, any, ParsedQs>”的参数。参数“req”和“req”的类型不兼容。类型“Request<ParamsDictionary,any,any,ParsedQs>”中缺少属性“user”,但类型“IRequest”中需要属性“user”。重载 2 of 3, …
我正在尝试 Dockerize 我的Strapi
应用程序,因此首先在项目的根目录中创建了一个.env
包含以下内容的文件:
HOST=0.0.0.0
PORT=3002
Run Code Online (Sandbox Code Playgroud)
然后,里面backend/config/server.js
有:
module.exports = ({ env }) => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT"),
admin: {
auth: {
secret: env("ADMIN_JWT_SECRET", "3b8efb990e54568fc0d91ff31390cda7"),
},
},
});
Run Code Online (Sandbox Code Playgroud)
该代码应该将应用程序绑定到0.0.0.0
. 事实上,当我运行容器时,我可以看到它绑定到0.0.0.0
.
之后,我创建了一个Dockerfile
包含以下说明的文件:
FROM node:12
EXPOSE 3002
WORKDIR /backend
COPY ./package.json .
RUN npm install
COPY . .
RUN ls -l
CMD ["npm", "run", "develop"]
Run Code Online (Sandbox Code Playgroud)
然后我有一个docker-compose.yml
:
version: '3'
services:
backend:
container_name: foo_backend
build: ./backend/
ports:
- '3002:3002'
volumes: …
Run Code Online (Sandbox Code Playgroud) 我是nodemailer
在用户注册后实现的,方法如下:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
let mailOptions = {
from: process.env.EMAIL_USERNAME,
to: user.email,
subject: 'Verify your account',
text: 'Click here for verify your account'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
Run Code Online (Sandbox Code Playgroud)
我不太喜欢这段代码,因为如果我需要在另一个中发送电子邮件module
,我需要重写上面的所有内容。
由于我是新来的NodeJS
,我想知道如果我能删除此代码冗余制造类似一个utility
或可能的helper
类。目标是导入wrapper
类并调用一个简单的函数来发送电子邮件。
哪个是最好的处理方法?
我是 Docker 的新手,我试图弄清楚是否可以使用不同的参数多次运行应用程序。
我使用 .Net 开发了我的应用程序,到目前为止我已经使用 Ubuntu 服务执行了所有实例,例如:
[Unit]
Description=swp %I
[Service]
Environment=ENV=PRODUCTION
Type=simple
User=foo
ExecStart=/usr/bin/dotnet /opt/swp/app.dll %i
WorkingDirectory=/opt/app
Restart=always
RestartSec=5
Run Code Online (Sandbox Code Playgroud)
%I
我在启动服务时传递的参数在哪里,例如:
sudo systemctl start app@argument1.service
sudo systemctl start app@argument2.service
sudo systemctl start app@argument3.service
Run Code Online (Sandbox Code Playgroud)
因此,根据提供的参数,我有三个以不同逻辑运行的应用程序实例。
如何使用 Docker 复制相同的内容?
假设我有这个字符串:/ban 1d hello world
它本质上是一个发送给机器人的命令,意思是:
/ban
指定禁止用户1d
代表时间hello world
是原因我需要得到reason
,所以我做到了:
let c = '/ban 1d hello world';
let arr = c.split(' ');
let result = c.replace(arr[0], '');
result = result.replace(arr[1], '')
alert(result);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我想问一下是否有更好的方法来实现这一目标。
亲切的问候。
我正在学习React
+Typescript
我正面临一个奇怪的问题。基本上我已经定义了一个FunctionComponent
:
const AddLogModal: React.FC<{ addLog: Function }> = ({ addLog }) => {
const [message, setMessage] = useState('');
const [attention, setAttention] = useState(false);
const [tech, setTech] = useState('');
Run Code Online (Sandbox Code Playgroud)
我试图分配给checkbox
值的attention
变量,所以我所做的:
<input
type="checkbox"
className="filled-in"
checked={attention}
value={attention}
onChange={e => setAttention(!attention)}
/>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我收到此错误:
类型 'boolean' 不能分配给类型 'string | 数量 | 只读字符串[] | 不明确的'。
我做错了什么?
express ×3
typescript ×3
docker ×2
javascript ×2
node.js ×2
nodemailer ×1
reactjs ×1
restify ×1
strapi ×1
typeorm ×1
validation ×1