代码之家  ›  专栏  ›  技术社区  ›  A.S.J

代理错误:无法将本地主机3000的请求/用户代理到http://localhost:3001/

  •  0
  • A.S.J  · 技术社区  · 6 年前

    这是我的ReactJS代码:

    class App extends React.Component {
    
    constructor(props) {
        super(props);
        //states and binding functions
    }
    
    fetchData () {
        fetch('/users')
            .then(res => res.json())
            .then(users => this.setState({users}));
    }
    
    addUser (event) {
    
        let fileData = new FormData();
    
        fileData.append("file", this.state.filesToBeSent);
    
        axios.post('adduser', 
            querystring.stringify({
              entry: this.state.currname,
              passwrd: this.state.currpasswrd,
              fileData : fileData
            })
                )
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
    
    }
    
    componentDidMount() {
        this.fetchData();
    }
    
    render() {
        return (
            <div className="App">
                <form onSubmit={this.addUser} encType="multipart/form-data">
                    <label>New username:</label>
                    <input value={this.state.currname} onChange={this.handleChange}></input>
                    <input value={this.state.currpasswrd} onChange={this.handlePassword} />
                    <input type="file" name="file" onChange={this.handleFile} />
                    <button type="submit" >Add</button>
                </form>
    
                <h1>Current Users: </h1>
                    {this.state.users.map((name, n) =>
                        //map through user-array and display names
                    )}
                </ul>
            </div>
        );
    }
    

    }

    (抱歉,如果代码太多,我会尽量缩短它,但我不确定哪些部分与问题相关)。

    以下是我如何在节点中接收数据,以及如何将部分数据保存到数据库:

    const storage = multer.diskStorage({
        destination: (req, file, cb) => {
            cb(null, "./uploads/");
        },
    
        filename: (req, file, cb) => {
            const newFilename = `${uuidv4()}${path.extname(file.originalname)}`;
            cb(null, newFilename);
        },
    })
    
    const upload = multer({ storage });
    
    router.post("/", upload.single("file"), (req, res) => {
        res.send(req.file + " and exit.");
        var newUser = new Todo();
            newUser.username = req.body.entry;
            newUser.passwrd = req.body.passwrd;
            newUser.save(function (err) {
                if(err) 
                    res.send(err);
                res.send('User added successfully!');
            });
    });
    

    这就是它变得奇怪的地方。在我插入 upload.single("file") 然而,我似乎不明白为什么。当我只有文本输入时,甚至当我创建 FormData() 等等。在我实现之前,它仍然工作得很好。

    我试着查找并实现发布在这里的答案,但似乎没有任何帮助。

    在屏幕上,我收到以下错误消息: Unhandled Rejection (SyntaxError): Unexpected token P in JSON at position 0 当我检查终端时,我收到标题中提到的错误消息。我尝试删除内容头(不确定这会做什么,但我所遵循的实现文件上载的教程没有使用内容头,这就是我尝试删除它们的原因。 有人知道如何修复此错误吗?

    编辑:终端中的错误消息还包含 ECONNRESET . 我跟踪了终端中的链接 https://nodejs.org/api/errors.html#errors_common_system_errors 但我仍然不确定我该如何解决这个问题。

    1 回复  |  直到 6 年前
        1
  •  0
  •   YouneL    6 年前

    我建议您将所有字段附加到 FormData 对象,并且不要将提交的数据转换为json字符串:

    let fileData = new FormData();
    
    fileData.append("entry", this.state.currname);
    fileData.append("passwrd", this.state.currpasswrd);
    fileData.append("file", this.state.filesToBeSent);
    
    axios.post('adduser', fileData)
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
        });