小编Jas*_*der的帖子

Jest/TypeORM 在所有测试后清除数据库

我想在所有笑话测试之后或之前删除数据库中的所有条目。

这是我的 setup.js:

import { getConnection, getConnectionManager } from "typeorm"

beforeAll(async () => {
    const connectionManager = getConnectionManager()
    const connection = connectionManager.create({
        "type": "postgres",
        "host": "localhost",
        "port": 54320,
        "username": "test",
        "password": "test",
        "database": "test",
        "entities": ["../src/entities/*.ts"],
        "logging": false,
        "synchronize": true
    })
    await connection.connect()
})

afterAll(async () => {
    await getConnection().close()
})
Run Code Online (Sandbox Code Playgroud)

我在 typeorm 文档中读到,“同步”选项会用空的新表覆盖旧表,但它似乎不起作用。

这是我所做的测试:

describe('POST /create', () => {
    it('Create a user', async () => {
        const user: IStringTMap<string> = {
            firstName: 'John',
            lastName: 'Doe',
            email: 'john.doe@test.com',
            password: 'test123!', …
Run Code Online (Sandbox Code Playgroud)

javascript testing jestjs typeorm

15
推荐指数
2
解决办法
2万
查看次数

如何让打字稿知道我的变量不再是未定义的

这是我的案例:

我正在使用Remix 加载器,它从 URL 获取参数,但参数定义为string | undefined

如果变量是undefined我想抛出一个重定向

export const loader: LoaderFunction = async ({ params }) => {
  const { myVar } = params; // myVar: string | undefined 
  definedOrRedirect(myVar, "/"); // Checks if undefined
  return fetchSomething(myVar); // myVar: string | undefined
};
Run Code Online (Sandbox Code Playgroud)

有没有办法让打字稿知道如果它没有抛出则myVar不是未定义的?

typescript

8
推荐指数
1
解决办法
1573
查看次数

Django - 认证()返回无

我阅读了很多关于这个问题的话题,问题总是存在。当我提交登录表单时,身份验证函数()返回 None。我使用函数 set_password() 进行注册,它没有任何改变。

这是我的代码(也在这里):

from django.contrib.auth.models import User
from django.shortcuts import render
from forms import LoginForm, RegistrationForm
from django.contrib import auth

def login(request):
    state = 0 # 0 = initialisation / 1 = OK / 2 = BAD_PW
    form = LoginForm()
    if request.method == 'POST':
        error = False
        if request.method == "POST":
            form = LoginForm(request.POST)
            if form.is_valid():
                email = form.cleaned_data["email"]
                password = form.cleaned_data["password"]
                user = auth.authenticate(email=email,
                                    password=password)
                if user:
                    auth.login(request, user)
                    state = 1
                else:
                    state = …
Run Code Online (Sandbox Code Playgroud)

python authentication django nonetype

1
推荐指数
1
解决办法
2646
查看次数