【问题标题】:How to post and parse data to node express如何将数据发布和解析到节点快递
【发布时间】:2018-06-25 17:51:24
【问题描述】:

我正在尝试将此 json 发布到 node express 端点

{
"data": [
    [
        "Audit Territory",
        "LA Antelope Valley",
        "LA Central",
        "LA East San Gabriel",
        "LA San Fernando Valley",
        "LA West",
        "LA West San Gabriel",
        "OR Inland, Coastal South",
        "OR West",
        "RV Central",
        "RV Coachella Valley",
        "RV South, Central",
        "SB High Desert",
        "Unassigned"
    ],
    [
        "Auditor Name",
        "Jeanna Bonds",
        "Dawn Wiley",
        "Janet Cortez",
        "Benjamin Sally",
        "Margie Watkins",
        "Jennifer Perich",
        "Tami Anderson",
        "Christy Brant",
        "Brian Lopiccolo",
        "Kristina Clark",
        "Tina Chester",
        "Ira Brown",
        " Unassigned"
    ],
    [
        "Not Started",
        20,
        13,
        24,
        25,
        24,
        52,
        117,
        33,
        48,
        54,
        44,
        69,
        2
    ],
    [
        "In Progress",
        1,
        2,
        0,
        1,
        1,
        1,
        1,
        0,
        0,
        0,
        18,
        0,
        0
    ],
    [
        "Could Not Complete",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
    ],
    [
        "Ready for Review",
        2,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        4,
        0,
        0
    ],
    [
        "Needs More Research",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
    ],
    [
        "Approved",
        1,
        0,
        0,
        1,
        1,
        0,
        2,
        0,
        1,
        1,
        3,
        3,
        0
    ]
],
"colWidths": [
    25,
    25,
    25,
    25,
    30,
    30,
    30,
    25
],
"colStyles": [
    {},
    {},
    {
        "horizontalAlignment": "center"
    },
    {
        "horizontalAlignment": "center"
    },
    {
        "horizontalAlignment": "center"
    },
    {
        "horizontalAlignment": "center"
    },
    {
        "horizontalAlignment": "center"
    },
    {
        "horizontalAlignment": "center"
    }
]
 }

它在 express 中没有被正确解析,我试图弄清楚需要什么。我尝试了几种不同的方法。

我安装了 body-parser 并全局应用了它

app.use(bodyParser.urlencoded({extended: true }))

这并没有改变任何东西。

*来自客户端的POST

    const _fetch = model => {
  return fetch(`http://0.0.0.0:9000/create-excels`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json; charset=utf-8'
    },
    body: JSON.stringify(model)
  }).then(statusHelper).then(response => response.json())
}

我尝试调整为此 api 自动生成的模型。

 const createExcelSchema = new Schema({
  data: {
    type: [[]] // Array
  },
  colWidths: {
    type: Array
  },
  colStyles: {
    type: [{}] // Array
  }
}, {
  timestamps: true,
  toJSON: {
    virtuals: true,
    transform: (obj, ret) => { delete ret._id }
  }
})

这确实影响了结果,但没有解决问题。这是我得到的结果

 {
    "data": [
        [
            "Audit Territory",
            "LA Antelope Valley",
            "LA Central",
            "LA East San Gabriel",
            "LA San Fernando Valley",
            "LA West",
            "LA West San Gabriel",
            [
                "OR Inland",
                "Coastal South"
            ],
            "OR West",
            "RV Central",
            "RV Coachella Valley",
            [
                "RV South",
                "Central"
            ],
            "SB High Desert",
            "Unassigned"
        ],
        [
            "Auditor Name",
            "Jeanna Bonds",
            "Dawn Wiley",
            "Janet Cortez",
            "Benjamin Sally",
            "Margie Watkins",
            "Jennifer Perich",
            "Tami Anderson",
            "Christy Brant",
            "Brian Lopiccolo",
            "Kristina Clark",
            "Tina Chester",
            "Ira Brown",
            "Unassigned"
        ],
        [
            "Not Started",
            "20",
            "13",
            "24",
            "25",
            "24",
            "52",
            "117",
            "33",
            "48",
            "54",
            "44",
            "69",
            "2"
        ],
        [
            "In Progress",
            "1",
            "2",
            "0",
            "1",
            "1",
            "1",
            "1",
            "0",
            "0",
            "0",
            "18",
            "0",
            "0"
        ],
        [
            "Could Not Complete",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0"
        ],
        [
            "Ready for Review",
            "2",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "4",
            "0",
            "0"
        ],
        [
            "Needs More Research",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0",
            "0"
        ],
        [
            "Approved",
            "1",
            "0",
            "0",
            "1",
            "1",
            "0",
            "2",
            "0",
            "1",
            "1",
            "3",
            "3",
            "0"
        ]
    ],
    "colWidths": "25,25,25,25,30,30,30,25",
    "colStyles": [
        "[object Object]",
        "[object Object]",
        "[object Object]",
        "[object Object]",
        "[object Object]",
        "[object Object]",
        "[object Object]",
        "[object Object]"
    ]
}

控制器

    export const create = ({ bodymen: { body } }, res, next) => {
  _createExcel(body.data, body.colWidths, body.colStyles).then(result => success(res.status(201).json(result)))
    .catch(next)
}

路线

      import { Router } from 'express'
import { middleware as body } from 'bodymen'
import { create } from './controller'
import { schema } from './model'
export CreateExcel, { schema } from './model'

const router = new Router()
const { data, colWidths, colStyles } = schema.tree

router.post('/',
  body({ data, colWidths, colStyles }),
  create)

型号

    import mongoose, { Schema } from 'mongoose'

const createExcelSchema = new Schema({
  data: {
    type: [[]]
  },
  colWidths: {
    type: Array
  },
  colStyles: {
    type: [{}]
  }
}, {
  timestamps: true,
  toJSON: {
    virtuals: true,
    transform: (obj, ret) => { delete ret._id }
  }
})

createExcelSchema.methods = {
  view (full) {
    const view = {
      // simple view
      id: this.id,
      data: this.data,
      colWidths: this.colWidths,
      colStyles: this.colStyles,
      createdAt: this.createdAt,
      updatedAt: this.updatedAt
    }

    return full ? {
      ...view
      // add properties for a full view
    } : view
  }
}

const model = mongoose.model('CreateExcel', createExcelSchema)

export const schema = model.schema
export default model

【问题讨论】:

  • 显示您的路线。您如何访问 request 对象上的变量?
  • 我刚刚添加了更多信息。
  • 你能告诉我你是如何发布这些数据来表达api的吗?您是否将内容类型标头设置为 application/x-www-form-urlencoded
  • 刚刚发布。我确实尝试将这个 app.use(bodyParser.urlencoded({ extended: true })) (bodyParser.urlencoded) 更改为 (bodyParser.json)
  • 在寻求帮助之前,我只是在尝试尽可能多的不同事物

标签: javascript json node.js express


【解决方案1】:

好吧,我假设您没有在 URL 中发布 JSON,这意味着应用

app.use(bodyParser.urlencoded({ ... }))

不会真正帮助你。您最可能想要的是 json 中间件,它将解析 JSON 格式的 body

app.use(bodyParser.json())

【讨论】:

  • 这是结果
  • [“或内陆”、“南海岸”]
  • 后者.. 实际值是“OR Inland, Coastal South”,它认为它是一个单独的数组
  • 我可以通过在发布之前将“,”替换为其他内容来解决此问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2016-06-04
相关资源
最近更新 更多