我正在使用multer上传文件。一切正常,但 fileFilter 不起作用。我不知道我哪里出错了。
路由.js
var multer = require('multer');
// Multer diskStorage setting
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads/');
},
filFilter: function (req, file, cb) {
var typeArray = file.mimetype.split('/');
var fileType = typeArray[1];
if (fileType == 'jpg' || fileType == 'png') {
cb(null, true);
} else {
cb(null, false)
}
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '_' + Date.now() + '.jpg');
}
});
var upload = multer({ storage: storage }); …Run Code Online (Sandbox Code Playgroud) 我正在使用 React 和 Redux 开发一个应用程序。我正在尝试使用 API 数据预填充我的输入。为此,我将 API 数据放入 useEffect 中。我也在其中设置我的状态。问题是输入未填充 API 数据。它是空的,但是当我重新加载页面时,它会被填充并且工作正常。
这是我的代码:
编辑问题.js
const EditQuestion = ({ getQuestionById, question: { question, loading }, match: { params } }) => {
const [formData, setFormData] = useState({ title: "" });
useEffect(() => {
// Get data from API
getQuestionById(params.id);
// Populate `title` input with API Data
setFormData({
title: question !== null && question.title ? question.title : ""
});
}, [getQuestionById, params.id]); // If `question` object is passed to this dependency …Run Code Online (Sandbox Code Playgroud) 我在 laravel 项目中使用 livewire 2.2 和 laravel 7.24。当我使用加载组件时
Route::get('/path', ComponentClassPath);
Run Code Online (Sandbox Code Playgroud)
它有效,但是当我在刀片视图中加载我的 livewire 组件时。例如,像这样的 test.blade.php:
@extends('layouts.app')
@section('content')
@livewire("my-livewire-component")
@endsection
Run Code Online (Sandbox Code Playgroud)
它不起作用并显示以下错误:
未定义变量:插槽(视图:E:\xampp\htdocs\lw\resources\views\layouts\app.blade.php)。
应用程序.blade.php
<head>
@livewireStyles
</head>
<body>
<div class="container">
{{ $slot }}
</div>
@livewireScripts
</body>
Run Code Online (Sandbox Code Playgroud)
任何解决方案?
由于我是反应的新手,因此我对反应状态的工作很少。更新简单状态时一切正常,但更新包含对象数组的状态时会出现问题。我在stackoverflow和其他网站上找到了一些相关的答案,但仍然无法找到更好的解决方案。
初始状态:
this.state = {
article: [{
title: '',
category: '',
author: '',
desc: '',
date: ''
},
{
authorName: '',
profile: '',
joinedOn: ''
}]
}
Run Code Online (Sandbox Code Playgroud)
更新状态:
<input type="text" name="title" placeholder="Enter article title" className="form-control" onChange={e => this.setState({article: [...this.state.article, {title: e.target.value}]})} />
Run Code Online (Sandbox Code Playgroud)
访问状态:
console.log(this.state.article[0].title)
Run Code Online (Sandbox Code Playgroud)
控制台显示空字符串。
我正在开发一个带有后端 firebase 的 React TODO 应用程序。我遇到的问题是,在从 Firebase RTD 更新或删除节点后,从 Firebase RTD 发送的数据会追加两次。
状态:
// Initialize state
this.state = {
todos: []
}
Run Code Online (Sandbox Code Playgroud)
这是我的getTodos函数,负责读取数据,在内部调用componentDidMount:
getTodos () {
firebase.database().ref('todos').on('value', (todo) => {
const todos = todo.val();
// Iterate through keys
Object.keys(todos).map(todoItem => {
let todo = todos[todoItem];
// Update the todos array
this.setState({
todos: [...this.state.todos, todo]
})
})
})
}
Run Code Online (Sandbox Code Playgroud)
渲染部分:
<tbody>
{
this.state.todos.map((todo, index) => {
return (
<tr key={index}>
<td>{index + 1}</td>
<td>{todo.title}</td>
<td>{todo.date}</td>
<td align="center"> …Run Code Online (Sandbox Code Playgroud) reactjs ×3
express ×1
firebase ×1
javascript ×1
laravel ×1
multer ×1
node.js ×1
php ×1
react-hooks ×1
states ×1
updating ×1
use-effect ×1