小编use*_*009的帖子

Knex.js和MySQL:将Integer转换为布尔值以进行批量选择

我使用Knex作为与MySQL数据库通信的服务器.

我有select语句可能会从数据库中返回大量记录.这些记录中的一些单元格是布尔值,这实际上意味着它们只是整数(0或1).在JavaScript中我需要它们作为布尔值,所以我可以将它们作为实际的'true'或'false'值而不是'0'和'1'以JSON格式发送.到目前为止,我发现的唯一解决方案是通过循环运行查询结果,将每个tinyint记录更改为布尔值.但是,我想知道,有没有办法配置查询构建器以自动返回某些单元格的布尔值?

javascript mysql node.js knex.js

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

Mongoose + Typescript 要求我扩展 Document

我正在尝试按照 Mongoose 文档中给出的说明来支持 TypeScript: https: //mongoosejs.com/docs/typescript.html

在文档中,他们提供了以下示例:

import { Schema, model, connect } from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface User {
  name: string;
  email: string;
  avatar?: string;
}

// 2. Create a Schema corresponding to the document interface.
const schema = new Schema<User>({
  name: { type: String, required: true },
  email: { type: String, required: true },
  avatar: String
});

// 3. Create a Model.
const UserModel = model<User>('User', schema);
Run Code Online (Sandbox Code Playgroud)

他们还提供了一个扩展接口的替代示例Document …

mongoose typescript

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

Next.js 页面和 API 调用的不同 404 页面

是否可以在 Next.js \xe2\x80\x94 中配置两个不同的 404 页面,一个用于渲染页面,另一个用于 API 路由?我希望 API 调用的 404 页面以 JSON 格式返回,而所有其他页面则以 HTML 格式返回。

\n

next.js

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

使用 Knex 在 MySQL 中进行条件 SELECT

有没有办法使用 Kenx 将 IF 语句添加到 SELECT 查询?假设我想实现以下查询:

SELECT id, name, IF(grade<60,'Fail','Pass') AS examResult FROM students;
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以在不使用 raw 的情况下用 Knex 做类似的事情?

谢谢。

knex.js

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

如何在 React 测试库中获取 Material-UI 密码输入

我在一个项目中使用 Material-UI 和 React 测试库,但我在 TextField 设置为type="password". 我知道,在测试中,默认使用testId字段获取元素不是一个好习惯,但我找不到进入密码输入字段的方法。我无法使用,getByRole因为显然<input type="password">没有作用。输入框中没有文本,也没有占位符文本,当我尝试使用时,getByLabelText我收到此错误消息:

TestingLibraryElementError: Found a label with the text of: Password, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.
Run Code Online (Sandbox Code Playgroud)

当我查看测试中渲染的元素时(我使用了screen.debug()),我意识到这就是 Material-UI 组成 TextField 的方式:

TestingLibraryElementError: Found a label with the text of: Password, however no form control was found associated to that label. Make sure you're using the …
Run Code Online (Sandbox Code Playgroud)

reactjs material-ui react-testing-library

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

Material-UI TextField onChange 不会在测试中调用函数

我正在学习 Jest 和 React,并尝试使用 Material-UI。我编写这个简单的测试只是为了检查事情如何工作:

import React from 'react';
import { render, fireEvent, configure} from '@testing-library/react';

import { TextField } from '@material-ui/core';

configure({ testIdAddribute: 'id' });

describe('Testing events', () => {

  test('onChange', () => {
    const onChangeMock = jest.fn();
    
    const { getByTestId } = render(
      <TextField id="test" onChange={onChangeMock} type="text" />
    );
    
    fireEvent.change(getByTestId('test', { target: { value: 'foo' } }));
    expect(onChangeMock.mock.calls.length).toBe(1);
  });
});
Run Code Online (Sandbox Code Playgroud)

但这个测试失败了:

expect(received).toBe(expected) // Object.is equality

    Expected: 1
    Received: 0
Run Code Online (Sandbox Code Playgroud)

我在这里不明白什么?

reactjs jestjs material-ui react-testing-library

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