我正在尝试将一些记录创建到表中,并且只填充了 createdAt 列。我希望 updatedAt 列在我创建模型时不存在,它会自动生成 createdAt 和 updatedAt 时间戳。
我尝试使用,timestamps : false
但 createdAt 列包含空值。
const MyTable = sequelize.define(
'my_table',
{
id: {
type: DataTypes.INTEGER(11),allowNull: false,
primaryKey: true,autoIncrement: true,
},
person_name: {type: DataTypes.STRING(255),allowNull: false},
created: {type: DataTypes.DATE,allowNull: true},
},
{
tableName: 'my_table',
timestamps: false,
createdAt: 'created',
},
);
Run Code Online (Sandbox Code Playgroud)
是否可以在模型本身中解决此问题而不对查询进行任何更改?
我有一个使用快速自定义浏览器用下一个js创建的博客应用程序。当我单击链接以路由到“ / blog / article-1”时,它会在到达该页面时刷新该页面。我如何避免这种情况?
server.js文件
const express = require('express');
const next = require('next');
const port = 80;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const compression = require('compression');
app.prepare()
.then(() => {
const server = express();
server.use(compression());
server.get('/blog', (req, res) => app.render(req, res, '/blog', req.query));
server.get('/blog/:postslug', (req, res) => {
const params = { postslug: req.params.postslug }
return app.render(req, res, '/blog/post', params)
});
server.get('*', (req, res) => handle(req, res));
server.listen(port, …
Run Code Online (Sandbox Code Playgroud) 我试图在Nextjs应用程序中更改路线时实现一个加载屏幕,例如/ home-> / about。
我当前的实现如下。我将初始加载状态设置为false,然后在componentDidMount上进行更改。我也调用Router.events.on内部功能componentDidMount路由变化开始时改变负荷状态。
_app.js在页面文件夹中
class MyApp extends App {
constructor(props) {
super(props);
this.state = {
loaded: false,
};
}
componentDidMount() {
this.setState({ loaded: true });
Router.events.on('routeChangeStart', () => this.setState({ loaded: false }));
Router.events.on('routeChangeComplete', () => this.setState({ loaded: true }));
}
render() {
const { Component, pageProps } = this.props;
const { loaded } = this.state;
const visibleStyle = {
display: '',
transition: 'display 3s',
};
const inVisibleStyle = {
display: 'none', …
Run Code Online (Sandbox Code Playgroud) 我有一个功能组件如下,我有一个有两个分派的功能。一个设置错误消息,另一个在短暂的超时时间后删除错误消息。设置错误和清除的功能存在于许多其他组件中,因此我希望在另一个文件中具有两个调度函数以允许代码重用。我无法做到这一点,因为我收到一个错误,说我只能在功能组件中使用 useDispatch。我如何克服这个?
组件
const Checkout = () => {
const dispatch = useDispatch();
const setErrors = (payload) => {
dispatch({
type: 'SET_ERRORS',
payload,
});
setTimeout(() => {
dispatch({
type: 'CLEAR_ERRORS',
});
}, 2500);
};
return (
<>
// JSX stuff come here
<>
)
}
Run Code Online (Sandbox Code Playgroud)
减速机
const initialState = {
message: null,
status: false,
};
export default (state = initialState, action) => {
switch (action.type) {
case 'SET_ERRORS':
return {
...state,
message: action.payload,
status: true,
};
case 'CLEAR_ERRORS':
return { …
Run Code Online (Sandbox Code Playgroud) 我有一个在react-select中显示的选项列表。由于此组件位于屏幕的下边缘,因此项目往往会从屏幕上被切除。
我注意到,官方提供的react-select中提供的选择列表具有滚动条。我曾尝试查看文档和GitHub页面,但仍然找不到解决方案。
下图显示了我的反应选择选项列表的显示方式。
我的问题是如何使列表显示为可滚动的。
以下是我使用的代码。
import Select from 'react-select';
const SearchSelect = (props) => {
const { options, defaultValue, onChange, name, label } = props;
return (
<>
{label && <label htmlFor="search-select">{label}</label>}
<Select
options={options}
defaultValue={defaultValue}
onChange={onChange}
name={name}
id="search-select"
/>
</>
);
};
Run Code Online (Sandbox Code Playgroud)
选项值最初是从API获取的,并从父级传递到此组件中。
我有一张类似于下表的表格。我试图找到今天所有价格的总和。
| id| price | created |
|---|-------|----------------------|
| 0 | 500 | 2018-04-02 11:40:48 |
| 1 | 2000 | 2018-04-02 11:40:48 |
| 2 | 4000 | 2018-07-02 11:40:48 |
Run Code Online (Sandbox Code Playgroud)
下面的代码是我想出来的,但它似乎不起作用。
const TODAY = new Date();
const SUM = await OrdersModel.sum('price', {
where: {
created: TODAY,
},
});
console.log(SUM);
Run Code Online (Sandbox Code Playgroud)
即使今天有条目,SUM 的值为 0。我也尝试了以下但它也没有奏效。
const TODAY = new Date();
const SUM = await OrdersModel.sum('price', {
where: {
created: Sequelize.DATE(TODAY),
},
});
console.log(SUM);
Run Code Online (Sandbox Code Playgroud)
在终端查询的SQL语句如下。
执行(默认):SELECT sum(`price`) AS `sum` FROM `orders` AS …
我试图将子组件的初始状态设置为父组件传递的 prop 值。
export default (props) => {
const myValueFromProp = props;
const [myValue, setMyValue] = useState(myValueFromProp);
return (
<Text>
{myValue}
</Text>
);
};
Run Code Online (Sandbox Code Playgroud)
以上是我想出的代码。我很困惑这是否是我应该遵循的正确方法。
在这个特定组件中有更多基于状态的逻辑,为了简单起见,我省略了这些逻辑。
如何使用 JavaScript 在 Web 应用程序上获取用户的电池百分比。
由于它是一个网络应用程序,因此它将在浏览器上运行。百分比将显示在网页上。
reactjs ×5
express ×3
javascript ×2
mysql ×2
next.js ×2
node.js ×2
react-hooks ×2
sequelize.js ×2
batterylevel ×1
browser ×1
function ×1
react-native ×1
react-select ×1
redux ×1