POST请求不允许数据库足够长的时间来生成ID

时间:2019-05-22 23:38:51

标签: reactjs postgresql asynchronous rails-api

我对rails-api的发布请求没有足够的时间来生成辅助ID。当我在setState函数(axios.post函数)的父容器中使用addNewLoad时,我的delivery_id(数据库中的定制辅助ID)返回nullthis.state.apiData中。

如果刷新页面,则delivery_id会填充正确的数据,但是返回response并在{{内使用setState时,我需要正确的数据1}}请求。

我不知道发生了什么事!

我正在使用axios到POSTGET的父容器:

POST

这是使用export default class PickupDeliveries extends Component { state = { apiData: [], } componentDidMount(){ // gets all the pickup/deliveries axios.get('http://localhost:3001/api/v1/pickup_deliveries') .then((response) => { console.log(response); this.setState({ apiData: response.data}) }) .catch((error)=>{console.log(error);}); } addNewLoad = (pickup_date, pickup_location, rate, delivery_date, delivery_location, local, loaded_miles, deadhead_miles, delivery_id, pickup_zip, delivery_zip) => { let config = { headers: { 'Content-Type': 'application/json; charset=utf-8', "Accepts": "application/json", } } const instance = axios.create({baseURL: 'http://localhost:3001'}) instance.post('/api/v1/pickup_deliveries', {pickup_date, pickup_location, rate, delivery_date, delivery_location, local, loaded_miles, deadhead_miles, delivery_id, pickup_zip, delivery_zip}, config) .then(response => { this.setState({apiData: [ ...this.state.apiData, response.data ] }) }) .catch(error => { console.log(error) }) } 提交表单数据的子组件:

this.props.addNewLoad

这是我的数据库:

    handleSubmit = (e) => {
        e.preventDefault()
        this.props.onNewLoad(
          this.state.pickup_date, 
          this.state.pickup_location, 
          this.state.rate, 
          this.state.delivery_date, 
          this.state.delivery_location, 
          this.state.local, 
          this.state.loaded_miles, 
          this.state.deadhead_miles, 
          this.state.delivery_id, 
          this.state.pickup_zip, 
          this.state.delivery_zip
          )
        this.setState({
            pickup_date: new Date(),
            pickup_zip: '',
            pickup_location: '',
            rate: '',
            delivery_zip: '',
            delivery_date: new Date(),
            delivery_location: '',
            loaded_miles: '',
            deadhead_miles: '',
            local: false,
          delivery_zip: '',
          association_zip: '',
          delivery_id: ''
        })
    }

这是我将增量功能设置为辅助ID的方法:

ActiveRecord::Schema.define(version: 2019_05_17_163434) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "pickup_deliveries", force: :cascade do |t|
    t.date "pickup_date"
    t.text "pickup_location"
    t.money "rate", scale: 2
    t.date "delivery_date"
    t.text "delivery_location"
    t.boolean "local"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "loaded_miles", default: 0
    t.integer "deadhead_miles", default: 0
    t.integer "delivery_id", default: -> { "nextval('delivery_id_seq'::regclass)" }
    t.text "pickup_zip"
    t.text "delivery_zip"
  end

end

我不知所措,我们将不胜感激。

编辑:

这是我的控制人:

class AddJobIdSequenceToScores < ActiveRecord::Migration[5.2]
  def up
    execute <<-SQL
      ALTER SEQUENCE delivery_id_seq OWNED BY pickup_deliveries.delivery_id;
      ALTER TABLE pickup_deliveries ALTER COLUMN delivery_id SET DEFAULT nextval('delivery_id_seq');
    SQL
  end

  def down
    execute <<-SQL
      ALTER SEQUENCE delivery_id_seq OWNED BY NONE;
      ALTER TABLE pickup_deliveries ALTER COLUMN delivery_id SET NOT NULL;
    SQL
  end
end

0 个答案:

没有答案