I want to send 3 arrays to Mongogodb Atlas but i'm having many problems to do it, i hope you can help me.
This is my code:
model: data.js:
'use strict'
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var dataSchema = Schema({
codDev: Number,
names: [String]
values: [String]
units: [String]
});
module.exports = mongoose.model('Data', dataSchema,'Data');
controller: project.js:
'use strict'
var newmodel = require('../models/data');
var controller = {
setData: function (req, res) {
var TestNMD = new newmodel();
TestNMD.codDispo = req.params.codDev;
TestNMD.nombres = req.params.names;
TestNMD.valores = req.params.values;
TestNMD.unidades = req.params.units;
if(req.params.codDev == null || req.params.names == null || req.params.values ==null || req.params.units ==null){
return res.status(400).send({
message: "Can't save."
});
}
TestNMD.save((ex, dataStored) => {
if (ex) return res.status(500).send({
message: "Error saving. " + ex
});
if (!dataStored) return res.status(400).send({
message: "Can't save"
});
return res.status(200).send({
data: dataStored
});
});
},
};
module.exports = controller;
routes: routes.js
'use strict'
var express = require('express');
var projectControler = require('../controllers/project.js');
var router = express.Router();
router.post('/setdata/:codDev?/:names?/:values?/:units?', projectControler.setData);
module.exports = router;
This is how i test the API: http://localhost:3700/api/setdata/1/[a,a]/[2,2]/[c,c]
First of all, when i test this on postman i get this error: "message": "Error saving. MongoWriteConcernError: No write concern mode named 'majority/' found in replica set configuration"
I found that this happens because of my connection string to mongodb Atlas, so had to delete this "test?retryWrites=true&w=majority/" to make it work. But i don't know if this could be troubling for another things. I want to know if it is possible to make it work without deleting that part of the string connection.
Also, the reason of the ask tittle is because when it sends the data it saves like this:
{
"data": {
"names": [
"[a,a]"
],
"values": [
"[2,2]"
],
"units": [
"[c,c]"
],
"_id": "5d93b98a69f98728e03c3d2f",
"codDev": 1,
"__v": 0
}
}
And i want to save the array like an array and not like a String.
So i've tried this:
var dataSchema = Schema({
codDev: Number,
names: [Array]
values: [Array]
units: [Array]
});
But i've got this:
{
"data": {
"names": [
[
"[a,a]"
]
],
"values": [
[
"[2,2]"
]
],
"units": [
[
"[c,c]"
]
],
"_id": "5d93ba6592eb87376cdc1e7d",
"codDev": 1,
"__v": 0
}
}
And all that i want is to get something like this:
{
"data": {
"names": [
[
"a",
"a"
]
],
"values": [
"2",
"2"
],
"units": [
[
"c",
"c"
]
],
"_id": "5d93ba6592eb87376cdc1e7d",
"codDev": 1,
}
}
Without the "__v": 0 too.
Thanks!