如何将配料数据发送到服务器?

时间:2016-05-31 11:13:32

标签: javascript node.js meteor reactjs ecmascript-6

我有一个配方表格,其中添加了更多的成分按钮,因为配方包含多种成分。我需要将这些数据传递给服务器。我正在使用Meteor作为后端。我可以将所有数据(如名称,描述,价格,估计交付)传递给服务器,但无法将配料发送到Recipe集合(或服务器)。如何将成分发送到服务器,因为它包含多个数据?

我的代码是

import React, { Component } from 'react';
import Input from './Input.jsx';

export default class Sell extends Component {
    constructor()
    {
        super();
        this.state = {
          inputValues : {},
          inputs:[]
        }
        this.onHandleSubmit =this.onHandleSubmit.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
    }

    onChange( name, { target : { value } })
    {
         const inputValues = this.state.inputValues;
         inputValues[name] = value;
         this.setState({ inputValues })
    }

    onHandleSubmit()
    {
        const name = `ingrediant-${this.state.inputs.length}`;
        let inputbox = <Input name={ name }
                              number={ this.state.inputs.length }   
                              key={ name }
                              onChange={ this.onChange.bind(this, name )} />

        const inputs = this.state.inputs;
        inputs.push(inputbox);
        this.setState({ inputs });
    }

    onSubmit(event){
        event.preventDefault();
        let nameOfRecipe = event.target.name.value; 
        let description = event.target.description.value; 
        let price = event.target.name.value; 
        let estimatedDelivery = event.target.delivery.value; 
        let ingredients = this.state.inputValues;
        Meteor.call('Recipe.insert', nameOfRecipe, description, price, estimatedDelivery, ingredients, (err) => {
            if(err){
                Materialize.toast(err.reason,2500);
            }else{
                FlowRouter.go('/');
            }
        });
    }

    render() {

        );
    }
}

server.js

Meteor.methods({
    'Recipes.insert':(nameOfRecipe, description, price, estimatedDelivery, ingredients) => {
        let recipe={
            user:Meteor.user(),
            createdAt:new Date(),
            nameOfRecipe:nameOfRecipe,
            description:description,
            price:price,
            estimatedDelivery:estimatedDelivery,
            ingredients:ingredients,
            likes:[],
            comments:[]
        }
        Recipes.insert(recipe);
    }
});
用于将数据发送到服务器的

代码位于onSubmit()。

0 个答案:

没有答案