如何在Meteor Mongodb的服务器端插入文档? [英] How can I insert document on server side for Meteor Mongodb?

查看:38
本文介绍了如何在Meteor Mongodb的服务器端插入文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢Nelson Yeung上载excel文件并插入到MongoDB中.接下来,我试图在React与MongoDB上实现此解决方案,并将文档插入服务器端.

Thanks to Nelson Yeung for uploading excel file and insert to MongoDB. Next, I'm trying to implement this solution on React with MongoDB and insert the document on the server side.

我已经完成了在客户端上看起来不错的代码,但是在服务器端上,它无法将文档插入到MongoDB中.有人可以帮我吗?

I'm done the code that look fine on client side but on the server side it cannot insert the document to the MongoDB. Could anybody help me please?

我的代码在这里:在客户端->

My code is here: On the client side ->

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { Slingshot } from 'meteor/edgee:slingshot';

const XLSX = require('xlsx');

class Upload extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isUploading: false,
        };

        this.handleUpload = this.handleUpload.bind(this);
    }

    handleUpload(event) {
        this.upload = new Slingshot.Upload('Upload');
        const file = event.target.files[0];
        const reader = new FileReader();
        reader.onload = function(e) {
            const data = e.target.result;
            const name = file.name;

            Meteor.call('upload', data, name, function(err, wb) {
                if(err) console.error(err);
                else {
                    document.getElementById('out').innerHTML = JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]),2,2);
                }
            });
        };
        reader.readAsBinaryString(file);
    }

    render() {
        return (
        <div className="Upload">
            <div>
            <input
                onChange={ this.handleUpload }
                type="file"
                name="Upload"
            />
            <p><i className="fa fa-cloud-upload" /> <span>Click or Drop Files to Upload</span></p>
            </div>
            <div id="out"></div>
        </div>
        );
    }   
}
Upload.propTypes = {};

export default Upload;

在服务器端->

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';

export const Registers = new Mongo.Collection('registers');
const XLSX = require('xlsx');
Meteor.methods({
    'upload': function upload(data, name) {
        const wb = XLSX.read(data, {type:'binary'});
        return wb;
        XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]).forEach(r => Registers.insert(r));

    },
});

推荐答案

我只是交换了返回和插入内容,所以效果很好.

I just swap return and insertion then it's working so nice.

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';

export const Registers = new Mongo.Collection('registers');
const XLSX = require('xlsx');
Meteor.methods({
    'upload': function upload(data, name) {
        const wb = XLSX.read(data, {type:'binary'});
        XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]).forEach(r => Registers.insert(r));
        return wb;
    },
});

这篇关于如何在Meteor Mongodb的服务器端插入文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆