将值数组传递给明确的POST路由

时间:2017-01-22 16:32:13

标签: arrays node.js express post mongoose

我有一个名为Todo的猫鼬模型,如下所示:

const string password = "123456789";    //just an example password

            string url = textBox1.Text;

            // Get if the user entered the right password
            GetPass pass = new GetPass(password);

            // Check this with a dialog result
            DialogResult result = pass.ShowDialog();

            if (result == DialogResult.OK)
            {
                    BlockList.Add(url);
                    MessageBox.Show("Added " + url + " to blocklist.");
                    textBox1.Clear();

            }

我明确的POST路线如下:

public partial class GetPass : Form
    {
        // Use a texBox called textBox1 and a button called btn_confirm
        private string refPassword;

        public GetPass(string password)
        {
            InitializeComponent();
            refPassword = password;
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            string password = textBox1.Text;
            if (password.CompareTo(refPassword) == 0)
            {
                this.DialogResult = DialogResult.OK;
            }
        }
    }

当我使用Postman发送int测试数据时,内容数组将返回空“content”:[]

我在邮递员中尝试了几种格式的身体,包括:

content: [{
    contentType: {
        type: String,
        default: null
    },
    contentValue: {
        type: String,
        default: null
    }
}]

app.post('/todos', authenticate, (req, res) => {
    var todo = new Todo({
        content: req.body.content

    });
    res.send(todo)
    //I am sending instead of sending the result for testing 
});

然而两者都以空数组回来。

{
    "content[contentType]": "reminder",
    "content[contentValue]": "Will it blend"
}

我是否需要在POST路径上更改某些内容以及\或以其他格式发送数据?

1 个答案:

答案 0 :(得分:0)

这里的内容是一个待办事项子文档,因此您应该像以下一样对待它:

app.post('/todos', authenticate, (req, res) => {
    var content = req.body.content;
    var todo = new Todo();

    todo.content.push(content);

    todo.save(function(err) {
      if (err) throw err;
      res.json(todo.toJSON())
      //I am sending instead of sending the result for testing 
    });

});

请参阅:Mongoose SubDocuments