Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用express.js和axios将数组插入mongodb_Node.js_Mongodb_Express_Vue.js_Axios - Fatal编程技术网

Node.js 如何使用express.js和axios将数组插入mongodb

Node.js 如何使用express.js和axios将数组插入mongodb,node.js,mongodb,express,vue.js,axios,Node.js,Mongodb,Express,Vue.js,Axios,我正在开发一个应用程序,其中我需要发送一个包含时间和食物量的数组。当我在前面(vuejs和axios)打印控制台日志时,我可以看到包含正确信息的阵列,但当我将该阵列发送到后面(express和mongoose)时,它作为未定义的阵列到达。我在前面下面提供我的功能和我后面的代码 我使用MongoDB作为数据库 你能帮帮我吗 前端:(vuejs) 快速代码: router.post('/register', (req, res) => { console.log(req.body.m

我正在开发一个应用程序,其中我需要发送一个包含时间和食物量的数组。当我在前面(vuejs和axios)打印控制台日志时,我可以看到包含正确信息的阵列,但当我将该阵列发送到后面(express和mongoose)时,它作为未定义的阵列到达。我在前面下面提供我的功能和我后面的代码

我使用MongoDB作为数据库

你能帮帮我吗

前端:(vuejs)

快速代码:

router.post('/register', (req, res) => { 
    console.log(req.body.mealList);
    console.log(req.user);
    const device = new Device({
        _id: new mongoose.Types.ObjectId(),
        mealList: [ req.body.mealList ],
        creation_information: {by: req.user._id}
    });
    console.log(device);
if (device.mealList && device.mealList.length) {
        device.save().then(result =>{
            console.log(result);
            res.status(201).json({
                message: "Hadling POST requests to device",
                createdDevice: result
            }); 

        })
        .catch(err =>{
            console.log(err);
            res.status(500).json({
                error: err
            })
        });
      }else {
          console.log('array is empty');
      }

});
我本想看到我的餐点排列的时间和数量,但我在我的终端上看到了这个

undefined
{ _id: 5cfbde0d5bd9cd0e168f14cf,
  mealList: [ undefined ],
  creation_information: { by: 'Lorena Meyas', date: 2019-06-08T16:10:53.756Z } }
{ _id: 5cfbde0d5bd9cd0e168f14cf,
  mealList: [ undefined ],
  creation_information: { by: 'Lorena Meyas', date: 2019-06-08T16:10:53.756Z },
  __v: 0 }
undefined
{ _id: 5cfbde0d5bd9cd0e168f14cf,
  mealList: [ undefined ],
  creation_information: { by: 'Lorena Meyas', date: 2019-06-08T16:10:53.756Z } }
{ _id: 5cfbde0d5bd9cd0e168f14cf,
  mealList: [ undefined ],
  creation_information: { by: 'Lorena Meyas', date: 2019-06-08T16:10:53.756Z },
  __v: 0 }