簡體   English   中英

對象數組未作為數組node.js接收

[英]Array of Objects not being received as an Array node.js

我正在傳遞一個包含“ wines”數組的POST。 發送外觀行上的對象。 但是,當我在服務器端記錄請求時,該數組是鍵和值的集合。 我缺少什么:

****我的測試要求代碼****

request.post("http://localhost:3000/todos", {form: params}, function(err, response){
    console.log(response.body);
})

****正在發送的選項****

var params = {
    name: 'Test Do',
    summary: 'This is the summary',
    wines: ['56ad66897070ffc5387352dc', '56dg66898180ffd6487353ef']
}

****我的服務器端代碼---為簡短起見***

exports.todoPost = function(req, res){
console.log(req.body);
var todo = new ToDo(req.body);
    todo.save(function(err, todoX){
        if(err){return res.send.err;}
        console.log(req.body.store_config)
        if(todo.wines){

****'console.log(要求的主體)的輸出****

{ name: 'Test Do',
  summary: 'This is the summary',
  'wines[0]': '56ad66897070ffc5387352dc',
  'wines[1]': '56dg66898180ffd6487353ef' }

我不能在POST中發送數組嗎? 我在網上看到的所有內容以及我嘗試過的所有內容均無效。 它說我正確地傳遞了東西。

從技術上講,您確實使用POST發送了一個數組。 它們的處理方式不同。 您可以做的一件事是將對象作為JSON字符串發送。

request.post("...", { form: JSON.stringify(params) }, function( ...

然后在服務器端,只需使用JSON.parse撤消字符串化即可。

var params = JSON.parse(req.body);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM