Reputation: 69
I am trying to use an array in a monogodb document with no success. My stack is node, mongodb, express, and will be consumed by react.
No matter how I've tried the other attributes of the document work fine, but the array attribute is always empty. Same results with my POST and PATCH methods/endpoints.
//Column.js
const mongoose = require('mongoose');
const ColumnSchema = new mongoose.Schema({
title: { type: String, required: true },
taskIds: { type: [String], required: true },
});
module.exports = mongoose.model('Column', ColumnSchema);
//columns.js
const express = require('express');
const router = express.Router();
const columnModel = require('../models/Column');
...
//create
router.route('/create').post(async (req, res) => {
const column = new columnModel({
title: req.body.title,
tasksIds: req.body.taskIds,
},
});
try {
const newColumn = await column.save();
res.status(201).json({ newColumn });
} catch (err) {
res.status(400).json({ message: err.message });
}
});
//update (change order, add taskId)
router.route('/update/:id').patch(async (req, res) => {
const id = req.params.id;
try {
const updatedColumn = await columnModel.findByIdAndUpdate(id, {
title: req.body.title,
tasksIds: req.body.taskIds,
});
res.status(201).json({ updatedColumn });
} catch (err) {
res.status(400).json({ message: err.message });
}
});
...
module.exports = router;
//call
POST http://localhost:8080/columns/create
Content-Type: application/json
{
"title": "city",
"taskIds": ["61279121db84bbff7adb5a93", "612799785fdb4004fc224c02"]
}
//response
HTTP/1.1 201 Created
X-Powered-By: Express
Access-Control-Allow-Origin: http://localhost:3001
Vary: Origin
Content-Type: application/json; charset=utf-8
Content-Length: 84
ETag: W/"54-N2SQU8p9MDDKOhyAZ39nPHnFZk4"
Date: Thu, 26 Aug 2021 14:55:02 GMT
Connection: close
{
"newColumn": {
"taskIds": [],
"_id": "6127ab461c45f70b087f51b1",
"title": "city",
"__v": 0
}
}
I have tried experimenting with my schema and defining the taskIds
with simpler syntax, I've experimented with using mongodb operators such as $addToSet: {tasksIds: req.body.taskIds }
No luck yet.
I cannot find any helpful examples using node. I must not understand some simple concept of arrays with mongo and could use some guidance. Much thanks!
Upvotes: 0
Views: 39
Reputation: 104
every thing look fine you just have a typo in column.js file : you should use taskIds insted of tasksIds
//in POST :
const column = new columnModel({
title: req.body.title,
taskIds: req.body.taskIds,
});
// in PATCH :
const updatedColumn = await columnModel.findByIdAndUpdate(id, {
title: req.body.title,
taskIds: req.body.taskIds,
});
the property name in your model is taskIds not tasksIds change these then your problem should be solved .
ps: another extra tip if you want to return new updated document after patch add new flag to true
const updatedColumn = await columnModel.findByIdAndUpdate(id, {
title: req.body.title,
taskIds: req.body.taskIds,
}, {new: true});
Upvotes: 2
Reputation: 2359
I think you forget to put $set
in the update query
const updatedColumn = await columnModel.findByIdAndUpdate(id, {$set:{
title: req.body.title,
tasksIds: req.body.taskIds,
}});
Upvotes: 0