Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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和node在mongoDB中上载阵列_Node.js_Mongodb_Mern - Fatal编程技术网

Node.js 使用express和node在mongoDB中上载阵列

Node.js 使用express和node在mongoDB中上载阵列,node.js,mongodb,mern,Node.js,Mongodb,Mern,我需要在mongoDB中插入一个数组,但始终会插入一个空数组。我做错了什么 我的模式 const taskSchema = new Schema({ username: { type: String, required: true }, title: { type: String, required: true }, subtasks: [String] //how to define array as type here?

我需要在mongoDB中插入一个数组,但始终会插入一个空数组。我做错了什么

我的模式

const taskSchema = new Schema({
    username: { type: String, required: true },
    title: { type: String, required: true },
    subtasks: [String]                        //how to define array as type here?
}, {
    timestamps: true,
});
待更新的数据:

{
    "username": "test",
    "title": "test",
    "subtasks": ["task1", "task2"]
}
更新:它现在可以工作了

我在路由文件中有一个错误,我在那里有“subtasksArray”而不是子任务:

const router = require('express').Router();
let TodaysTask = require('../models/todaysTask.model');

router.route('/add').post((req, res) => {
  const username = req.body.username;
  const title = req.body.title;
  const subTaskArray = req.body.subtasksArray;    //it should be subtasks here

  const newTodaysTask = new TodaysTask({
    username,
    title,
    subtasksArray      
  });

  newTodaysTask.save()
  .then(() => res.json('Task added!'))
  .catch(err => res.status(400).json('Error: ' + err));
});

您定义了属性
任务
,但正在尝试插入
子任务


也许定义属性
子任务

您试图插入的代码会很有帮助。架构定义看起来不错。嗨,我正在通过邮递员传递要更新的数据。一切正常,但子任务总是空的([]),它应该是一个带有task1和task2的数组,不是吗?你能发布更多与这个问题相关的代码吗?接收HTTP方法的函数,以及用于持久化数据的方法。谢谢,它现在工作了。我在路由文件中出错-使用子任务数组而不是子任务。我在问题中犯了一个错误。。。抱歉,这是子任务(两个)。。。