I don't understand how to use the findOrCreate method with mongoose











up vote
0
down vote

favorite












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 :




  1. Find the documents for which the the list contains param1 and return their titles in an array.


  2. 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.










share|improve this question
























  • try this link: stackoverflow.com/questions/7334390/…
    – Alok Deshwal
    Nov 10 at 16:30















up vote
0
down vote

favorite












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 :




  1. Find the documents for which the the list contains param1 and return their titles in an array.


  2. 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.










share|improve this question
























  • try this link: stackoverflow.com/questions/7334390/…
    – Alok Deshwal
    Nov 10 at 16:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 :




  1. Find the documents for which the the list contains param1 and return their titles in an array.


  2. 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.










share|improve this question















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 :




  1. Find the documents for which the the list contains param1 and return their titles in an array.


  2. 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.







javascript node.js methods mongoose find






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 16:50









Yannick Loriot

5,92022545




5,92022545










asked Nov 10 at 16:06









Tao77

53




53












  • try this link: stackoverflow.com/questions/7334390/…
    – Alok Deshwal
    Nov 10 at 16:30


















  • try this link: stackoverflow.com/questions/7334390/…
    – Alok Deshwal
    Nov 10 at 16:30
















try this link: stackoverflow.com/questions/7334390/…
– Alok Deshwal
Nov 10 at 16:30




try this link: stackoverflow.com/questions/7334390/…
– Alok Deshwal
Nov 10 at 16:30












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










In your cap.controller.js there are many errors. Firstly why the foc method is outside the cap_get one? And secondly you never call the foc method, why?



In your case you want to check whether a Cap with a title given by the params exists and if this is not the case create and it.



You can simply achieve that like that:



exports.cap_get = function (req, res, next) {
let capTitle = req.params.param1;

Cap.findOrCreate({ title: capTitle }, function(err, cap) {
if (err) return next(err);

console.log(capTitle + ' has been created !');

res.status(200).json({ title: capTitle });
});
};


The cap_get is just an express callback with its (req, res, next) signature, and the findOrCreate first parameter is the primary key, so here if no caps exist with this key (the title) it'll create it else it'll return the document.



You'll have to ensure that the cap's title is unique to avoid potential issues:



let CapSchema = new Schema ({
title: {type: String, unique: true},
list: {type: Array}
});





share|improve this answer





















  • thx, I mixed up all the mongoose methods together and couldn't figure out how to extract one element of the document without doing a callback.
    – Tao77
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240788%2fi-dont-understand-how-to-use-the-findorcreate-method-with-mongoose%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










In your cap.controller.js there are many errors. Firstly why the foc method is outside the cap_get one? And secondly you never call the foc method, why?



In your case you want to check whether a Cap with a title given by the params exists and if this is not the case create and it.



You can simply achieve that like that:



exports.cap_get = function (req, res, next) {
let capTitle = req.params.param1;

Cap.findOrCreate({ title: capTitle }, function(err, cap) {
if (err) return next(err);

console.log(capTitle + ' has been created !');

res.status(200).json({ title: capTitle });
});
};


The cap_get is just an express callback with its (req, res, next) signature, and the findOrCreate first parameter is the primary key, so here if no caps exist with this key (the title) it'll create it else it'll return the document.



You'll have to ensure that the cap's title is unique to avoid potential issues:



let CapSchema = new Schema ({
title: {type: String, unique: true},
list: {type: Array}
});





share|improve this answer





















  • thx, I mixed up all the mongoose methods together and couldn't figure out how to extract one element of the document without doing a callback.
    – Tao77
    2 days ago















up vote
0
down vote



accepted










In your cap.controller.js there are many errors. Firstly why the foc method is outside the cap_get one? And secondly you never call the foc method, why?



In your case you want to check whether a Cap with a title given by the params exists and if this is not the case create and it.



You can simply achieve that like that:



exports.cap_get = function (req, res, next) {
let capTitle = req.params.param1;

Cap.findOrCreate({ title: capTitle }, function(err, cap) {
if (err) return next(err);

console.log(capTitle + ' has been created !');

res.status(200).json({ title: capTitle });
});
};


The cap_get is just an express callback with its (req, res, next) signature, and the findOrCreate first parameter is the primary key, so here if no caps exist with this key (the title) it'll create it else it'll return the document.



You'll have to ensure that the cap's title is unique to avoid potential issues:



let CapSchema = new Schema ({
title: {type: String, unique: true},
list: {type: Array}
});





share|improve this answer





















  • thx, I mixed up all the mongoose methods together and couldn't figure out how to extract one element of the document without doing a callback.
    – Tao77
    2 days ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






In your cap.controller.js there are many errors. Firstly why the foc method is outside the cap_get one? And secondly you never call the foc method, why?



In your case you want to check whether a Cap with a title given by the params exists and if this is not the case create and it.



You can simply achieve that like that:



exports.cap_get = function (req, res, next) {
let capTitle = req.params.param1;

Cap.findOrCreate({ title: capTitle }, function(err, cap) {
if (err) return next(err);

console.log(capTitle + ' has been created !');

res.status(200).json({ title: capTitle });
});
};


The cap_get is just an express callback with its (req, res, next) signature, and the findOrCreate first parameter is the primary key, so here if no caps exist with this key (the title) it'll create it else it'll return the document.



You'll have to ensure that the cap's title is unique to avoid potential issues:



let CapSchema = new Schema ({
title: {type: String, unique: true},
list: {type: Array}
});





share|improve this answer












In your cap.controller.js there are many errors. Firstly why the foc method is outside the cap_get one? And secondly you never call the foc method, why?



In your case you want to check whether a Cap with a title given by the params exists and if this is not the case create and it.



You can simply achieve that like that:



exports.cap_get = function (req, res, next) {
let capTitle = req.params.param1;

Cap.findOrCreate({ title: capTitle }, function(err, cap) {
if (err) return next(err);

console.log(capTitle + ' has been created !');

res.status(200).json({ title: capTitle });
});
};


The cap_get is just an express callback with its (req, res, next) signature, and the findOrCreate first parameter is the primary key, so here if no caps exist with this key (the title) it'll create it else it'll return the document.



You'll have to ensure that the cap's title is unique to avoid potential issues:



let CapSchema = new Schema ({
title: {type: String, unique: true},
list: {type: Array}
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 16:48









Yannick Loriot

5,92022545




5,92022545












  • thx, I mixed up all the mongoose methods together and couldn't figure out how to extract one element of the document without doing a callback.
    – Tao77
    2 days ago


















  • thx, I mixed up all the mongoose methods together and couldn't figure out how to extract one element of the document without doing a callback.
    – Tao77
    2 days ago
















thx, I mixed up all the mongoose methods together and couldn't figure out how to extract one element of the document without doing a callback.
– Tao77
2 days ago




thx, I mixed up all the mongoose methods together and couldn't figure out how to extract one element of the document without doing a callback.
– Tao77
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240788%2fi-dont-understand-how-to-use-the-findorcreate-method-with-mongoose%23new-answer', 'question_page');
}
);

Post as a guest