Following this Schema cap.model.js :
const mongoose = require('mongoose');
const findOrCreate = require ('mongoose-findorcreate')
const Schema = mongoose.Schema;
let CapSchema = new Schema ({
title: {type: String},
list: {type: Array}
});
CapSchema.plugin(findOrCreate);
module.exports = mongoose.model('Cap', CapSchema);
I want to retrieve data from a get('/url/:param1') with the following datas :
Find the documents for which the the list contains param1 and return their titles in an array.
Find the document which title is param1, and return its list. Or if it doesn't exist, create the document. And in both case, also return its title.
Then I want to console.log : The title (2) on one side, and the array formed by the array of titles (1) and the list (2) on the other.
My problems are that I can't find or don't understand the method findOrCreate (https://www.npmjs.com/package/mongoose-findorcreate) and which arguments I have to use.
My code looks like this for the moment :
In route.js :
const express = require('express');
const router = express.Router();
const cap_controller = require('../controllers/cap.controller');
router.get('/:param1', cap_controller.cap_get);
module.exports = router;
In cap.controller.js:
const Cap = require('../models/cap.model');
const foc = Cap.findOrCreate({},
function(err, cap) {
console.log(req.params.word + ' has been created !', word.main);
},
'title list');
exports.cap_get = function (req, cb) {
let capTitle = req.params.param1;
cb(capTitle);
};
And I'm stuck with not understanding when does a method need arguments and what kind of arguments does a callback function need.