2016-05-31 48 views
0

我对JS比较陌生,并且正在做出反应,我正在使用Redux-Form将一个简单的项目添加到我的mongo数据库中。我配置了表单并且使用db连接定义了API,但是我不认为我正确地在动作中构造了我的代码,以便它可以用可以解释的方式发送数据。构建Ajax调用Redux表单动作来表达Mongo

具体来说,我看到按提交后的表单数据,但得到500错误。有人可以指示我适当地塑造我的ajax请求的方向,以便它接收数据库的可用数据?

enter image description here

新项目形式:

import React, { Component } from 'react'; import { fetchItems } from '../actions/index'; import {reduxForm} from 'redux-form'; import { createItem } from '../actions/index'; 
 

 

 
class NewItem extends Component { 
 

 
     render() { 
 

 

 
      const { fields: {name, category, description, image}, handleSubmit } = this.props; 
 

 

 
      return (
 
      <form onSubmit={handleSubmit(this.props.createItem)}> 
 
       <h3>Add an Item</h3> 
 
       <div className="form-group"> 
 
        <label>Item Name</label> 
 
        <input type="text" className="form-control" {...name} /> 
 
       </div> 
 
       <div className="form-group"> 
 
        <label>Category</label> 
 
        <input type="text" className="form-control" {...category} /> 
 
       </div> 
 
       <div className="form-group"> 
 
        <label>Description</label> 
 
        <textarea className="form-control" {...description} /> 
 
       </div> 
 
       <div className="form-group"> 
 
        <label>Image URL</label> 
 
        <input type="text" className="form-control" {...image} /> 
 
       </div> 
 
       <button type="submit" className="btn btn-primary">Submit</button> 
 
      </form> 
 
      ) 
 
     } 
 

 
} 
 

 
export default reduxForm({ 
 
    form: 'NewItemForm', 
 
    fields: ['name', 'category', 'description', 'image'] }, null, {createItem})(NewItem);

行动

export const FETCH_MY_ITEMS = 'FETCH_MY_ITEMS'; 
 
export const FETCH_SEARCH_ITEMS = 'FETCH_SEARCH_ITEMS'; 
 
export const CREATE_ITEM = 'CREATE_ITEM'; 
 
export const FETCH_ITEM = 'FETCH_ITEM'; 
 
export const DELETE_ITEM = 'DELETE_ITEM'; 
 

 
export function createItem (props) { 
 

 
    const request = $.post('/newItem', props).then(function (result){ 
 
    return { 
 
     result 
 
    } 
 
}); 
 

 
    return { 
 
     type: CREATE_ITEM, 
 
     payload: request 
 
    }; 
 
}

表达index.js为的newitem

app.post("/newItem", function(req, res) { 
     var newItem = require('./server/api/new-item')(dbConnection); 
     newItem(req, res); 
    }); 

API端点

var assert = require('assert'); 

//add new item to 
module.exports = function (dbConnection) { 
    return function (req, res) { 

     var newItem = req.body.newItem; 
     newItem.user = req.session.userId; 
     newItem.createDate = new Date; 
     dbConnection.collection('items').insertOne(newItem, function (err, result) { 
      if (err) { 
       res.send("error: something bad happened and your item was not saved") 
      } 
      res.send("success-item-posted"); 
      res.end(); 

     }) 
    } 
}; 

终端错误:类型错误:未定义 无法设置属性 '用户' 在/用户/玛丽/工作文件/在/Users/mary/working-files/closet-redux/index.js:184:13 位于Layer.handle [作为handle_request](/ /服务器/ api /新项目.js:13:22 ) Users/mary/working-files/closet-redux/node_modules/express/lib/router/layer.js:95:5)(/ Users/mary/working-files/closet-path)下的(/Users/mary/working-files/closet-redux/node_modules/express/lib/router/route.js:131:13)上的 在/etc/mysql/remove/modules/express/lib/remove/model/modules/express/lib/routent/layer.php上使用下面的命令:redis/redis/redis/redis/redis/redis/.js:95:5) at /Users/mary/working-files/closet-redux/node_modules/express/lib/router/index.js:277:22 at Function.process_params(/ Users/mary/working- files/closet-redux/node_modules/express/lib/router/index.js:330:12) at next(/Users/mary/working-files/closet-redux/node_modules/express/lib/router/index.js :271:10) at Immediate。 (/Users/mary/working-files/closet-redux/node_modules/express-session/index.js:432:7)

+0

在你的动作中,console.log(result)返回什么结果?那里可能有一个有价值的错误信息。另外,为什么不在$ .post上使用'fetch'。他们通常都是一样的工作,但我发现fetch很适合承诺。我也想知道是否添加'.catch(error)'也会帮助捕获错误。 让我知道这是否有帮助 – bdougie

+0

我还没有做过任何,因为我是一个新手,不知道他们:)我刚刚添加控制台日志 - 并添加终端错误的问题。 –

+0

看起来像我有一个用户的问题...我应该让我登录的用户在与userId存储的会话...我会进一步了解这一点。 –

回答

0

感谢@bdougie,我仔细查看了服务器的控制台日志,发现2问题:

1)形式发送对象,而不是命名对象所以只需要使用req.body我的newitem可变

时加入的newitem 2)从会话用户id是给错误 - 和老实说,我刚搬到它低于创建日期并使用console.log进行测试,现在它正在工作。