【问题标题】:How to generate a unique id automatically for the data that is posted to an API server using axios library in redux如何使用 redux 中的 axios 库为发布到 API 服务器的数据自动生成唯一 ID
【发布时间】:2018-06-10 09:41:21
【问题描述】:

我有一个 redux-form,用于从用户接收数据并将其发布到后端 API 服务器。在将数据发布到服务器时,我想为每组数据生成一个唯一的 id被发布到服务器,以便我以后可以使用该 ID 来引用该特定集合并将其显示给用户。那么,如何在将一组数据发布到服务器时自动生成该唯一 ID?

我的 redux-form 的代码如下:

import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { createPosts } from '../actions/posts_action';

class CreatePost extends Component {
  constructor() {
    super();
    this.state = {
      selectValue : ''
  };
  this.handleChange = this.handleChange.bind(this);
  this.renderCategory = this.renderCategory.bind(this);
}

  renderField(field) {
      return(
        <div className="title-design">
            <label className="label-design"> {field.label} </label>
            <input
              type="text"
              className="title-input"
              {...field.input}
            />
            <div className="text-help  has-danger">
              {field.meta.touched ? field.meta.error : ''}
            </div>
      </div>
      );
  }

  handleChange(e) {
    const value=e.target.value;
    this.props.change("categories",value);
    this.setState({selectValue: value}, () => {
      console.log(value)
    });
  }

  renderCategory(field) {
    return(
      <div className="title-design">
        <label className="label-design">{field.label} </label>
          <Field name="category" className="title-input" component="select">
            <option></option>
            <option value="react">React</option>
            <option value="redux">Redux</option>
            <option value="udacity">Udacity</option>
          </Field>

          <div className="text-help has-danger">
            {field.meta.touched ? field.meta.error : ''}
          </div>
      </div>
    );
  }

    onSubmit(values) {
      this.props.createPosts(values, () => {
          this.props.history.push('/');
      });
    }



    render() {
      const { handleSubmit } = this.props;

      return (
        <form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
          <Field
            label="Title for Post"
            name="title"
            component={this.renderField}
          />

          <Field
            label="Post Content"
            name="body"
            component={this.renderField}
          />

          <Field
            label="Category"
            name="category"
            component={this.renderCategory}
            />



          <button type="submit" className="btn btn-primary">Submit</button>
          <Link  to="/">
            <button className="cancel-button">Cancel</button>
          </Link>
        </form>
      );
    }
}

function validate(values) {
  const errors = {} ;

  if (!values.title) {
      errors.title = "Enter a title";
  }

  if (!values.body) {
    errors.body = "Enter some content";
    }

  if(!values.category) {
    errors.category = "Please select a category";
  }

  if (!values.id) {
      errors.id = "ID not generated";
  }

  return errors;
}

export default reduxForm({
  validate : validate,          //validate
  form : 'CreatePostForm'
})(
  connect(null,{ createPosts })(CreatePost)
);

创建新帖子的动作创建者如下:

//Action Creator for creating posts
export function createPosts(values, callback) {

  return dispatch => {
    return axios.post(`${API}/posts`,values,{headers})
      .then((data) => {
        callback();
        console.log(data)
      dispatch({
        type: CREATE_POST,
        payload: data
      })
    })
  }
}

Reducer 用于创建帖子:

import _ from 'lodash';
import { FETCH_POSTS, FETCH_POST, CREATE_POST } from '../actions/posts_action';

export default function(state = {}, action) {
  switch (action.type) {
    case FETCH_POST:
      // const post = action.payload.data;
      // const newState  = { ...state,  };
      // newState[post.id] = post;
      // return newState;
      return {...state, [action.payload.id]: action.payload};

    case FETCH_POSTS:
     return {posts: { ...state.posts, ...action.payload }};

    case CREATE_POST:
      return {posts: { ...state, ...action.payload}};

     default:
      return state;
  }

}

那么,如何为发送到服务器的每组数据生成id?

编辑 1:

我正在尝试在我的 onSubmit() 方法中添加 uuid,如下所示,但它不起作用。我不知道应该如何完成,或者我必须以其他方式完成?

  onSubmit(values) {

          this.props.createPosts(values, () => {
            uuidv1();
          this.props.history.push('/');
      });
    }

编辑 2:

帖子的服务器文件:

const clone = require('clone')

let db = {}

const defaultData = {
  "8xf0y6ziyjabvozdd253nd": {
    id: '8xf0y6ziyjabvozdd253nd',
    timestamp: 1467166872634,
    title: 'Udacity is the best place to learn React',
    body: 'Everyone says so after all.',
    author: 'thingtwo',
    category: 'react',
    voteScore: 6,
    deleted: false,
    commentCount: 2
  },
  "6ni6ok3ym7mf1p33lnez": {
    id: '6ni6ok3ym7mf1p33lnez',
    timestamp: 1468479767190,
    title: 'Learn Redux in 10 minutes!',
    body: 'Just kidding. It takes more than 10 minutes to learn technology.',
    author: 'thingone',
    category: 'redux',
    voteScore: -5,
    deleted: false,
    commentCount: 0
  }
}

function getData (token) {
  let data = db[token]
  if (data == null) {
    data = db[token] = clone(defaultData)
  }
  return data
}

function getByCategory (token, category) {
  return new Promise((res) => {
    let posts = getData(token)
    let keys = Object.keys(posts)
    let filtered_keys = keys.filter(key => posts[key].category === category && !posts[key].deleted)
    res(filtered_keys.map(key => posts[key]))
  })
}

function get (token, id) {
  return new Promise((res) => {
    const posts = getData(token)
    res(
      posts[id].deleted
        ? {}
        : posts[id]
    )
  })
}

function getAll (token) {
  return new Promise((res) => {
    const posts = getData(token)
    let keys = Object.keys(posts)
    let filtered_keys = keys.filter(key => !posts[key].deleted)
    res(filtered_keys.map(key => posts[key]))
  })
}

function add (token, post) {
  return new Promise((res) => {
    let posts = getData(token)

    posts[post.id] = {
      id: post.id,
      timestamp: post.timestamp,
      title: post.title,
      body: post.body,
      author: post.author,
      category: post.category,
      voteScore: 1,
      deleted: false,
      commentCount: 0
    }

    res(posts[post.id])
  })
}

function vote (token, id, option) {
  return new Promise((res) => {
    let posts = getData(token)
    post = posts[id]
    switch(option) {
        case "upVote":
            post.voteScore = post.voteScore + 1
            break
        case "downVote":
            post.voteScore = post.voteScore - 1
            break
        default:
            console.log(`posts.vote received incorrect parameter: ${option}`)
    }
    res(post)
  })
}

function disable (token, id) {
    return new Promise((res) => {
      let posts = getData(token)
      posts[id].deleted = true
      res(posts[id])
    })
}

function edit (token, id, post) {
    return new Promise((res) => {
        let posts = getData(token)
        for (prop in post) {
            posts[id][prop] = post[prop]
        }
        res(posts[id])
    })
}

function incrementCommentCounter(token, id, count) {
  const data = getData(token)
  if (data[id]) {
    data[id].commentCount += count
  }
}

module.exports = {
  get,
  getAll,
  getByCategory,
  add,
  vote,
  disable,
  edit,
  getAll,
  incrementCommentCounter
}

【问题讨论】:

  • 一般由服务器负责生成唯一的 id 并将其与响应数据一起传递
  • @ShubhamKhatri 当我尝试创建新帖子时,我没有看到帖子的任何“id”属性。我在编辑中粘贴了上面的服务器文件。你能看看它。

标签: reactjs redux axios


【解决方案1】:

好吧,通常您会让服务器生成该 ID,然后将其作为 axios 调用响应的一部分发送回去,但如果您真的想在客户端上生成 ID,我建议使用 uuid 和一个它的各种 UUID 生成器: https://www.npmjs.com/package/uuid

文档中的示例:

const uuidv4 = require('uuid/v4');
uuidv4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' 

您添加了更多代码,我认为在您的服务器上使用 uuidv4() 会更好地为您服务;类似:

function add (token, post) {
  return new Promise((res) => {
    let posts = getData(token)
    let id = uuidv4();

    posts[id] = {
      id: id,
      timestamp: post.timestamp,
      title: post.title,
      body: post.body,
      author: post.author,
      category: post.category,
      voteScore: 1,
      deleted: false,
      commentCount: 0
    }

    res(posts[id])
  })
}

这样,id就在服务器上生成了。客户端不再需要依赖于 uuid,并且您不会“信任”客户端生成有效的 id。 id 将作为响应的一部分呈现给客户端,然后您可以在进行编辑等时从客户端使用它。

【讨论】:

  • 感谢您的回复。让我试试看。几分钟后会更新您的结果。
  • 我在导入后尝试在我的onSubmit方法中添加uuidv1(),但我没有看到它添加了。我在上面的编辑中添加了代码。你能看一下吗并告诉我如何进行?
  • 我以为你想提交身份证?您只是在调用该方法,而不是使用生成的 ID 值。您可能希望将 uuidv1() 的结果添加到被推送的值中,以便服务器可以将其作为结果的一部分立即发送回来。 “值[id] = uuidv1();”在进行 axios 调用之前。
  • @Rashomon 是的,有......每次你的 redux 代码重新启动时(当你重新部署时,或其他),你的计数器回到 0,你得到你上次创建的重复时间。
  • @Rashomon 我有点假设生成 ID 的主要原因是将对象存储在某种数据库中,以便以后能够引用它。如果它只是一个临时标识符(如会话),那么您的计数器将起作用。毕竟,这就是 DB 序列的作用。
猜你喜欢
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多