cfs集合显示react.component中的错误

时间:2018-11-23 06:39:55

标签: javascript reactjs meteor react-component cfs

我有cfs集合用于文件上传。我已经在模板中使用了它,并且效果很好。现在,我必须使用此react.component,但它显示此错误Uncaught ReferenceError: fsFile is not defined。我添加了FS.Collection,但是它仍然显示错误。当我使用模板时它可以工作,但不能在react组件中工作。我该如何解决?

import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';

import React from 'react';
import Switch from 'react-switch';
import {Events} from './../../api/events';
import DatePicker from "react-datepicker";
import moment from "moment";
import "react-datepicker/dist/react-datepicker.css";

export default class AddEvent extends React.Component{
    EventForm(e){
        e.preventDefault();
        let eventTitle = e.target.title.value;
        let file = $('#projectImage').get(0).files[0];


         if(file){
            fsFile = new FS.File(file);
            ProjectImages.insert(fsFile, function(err, result){
                if(!err){
                    var projectImage = '/cfs/files/ProjectImages/' + result._id;

                    // Insert Project
                    Events.insert({
                        name: eventTitle,
                        projectImage: projectImage
                    });
                }
            });
        } else {
            // Insert Project
            Events.insert({
                name: eventTitle
            });
        }

    }

    render(){
        return(
            <div className="tab-pane fade" id="upEvents">
                <form className="upload-event-from" onSubmit={this.EventForm.bind(this)}>
                    <div className="form-group">
                        <input type="text" className="form-control" id="event_title" placeholder="Event Title" name="title" />
                        <span className="error-message eventTitle"></span>
                    </div>
                    <div className="form-group fg-icon">
                        <label>Upload Image</label>
                        <input type="file" className="form-control" name="projectImage" id="projectImage" />
                        <img id="blah" src="#" alt="your image"  />
                    </div>

                    <center>
                        <button type="submit" className="btn app-btn">Publish</button>
                    </center>
                </form>
            </div>
        );
    }
}

1 个答案:

答案 0 :(得分:1)

在该fsFile = new FS.File(file);行中的问题,您没有声明let,但是在此行的下方您再次调用了此命令,我希望从那里开始发生此问题,请像下面这样使用

let fsFile = new FS.File(file);
ProjectImages.insert(fsFile, function(err, result){
    ....