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 :
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.
javascript node.js methods mongoose find
add a comment |
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 :
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.
javascript node.js methods mongoose find
try this link: stackoverflow.com/questions/7334390/…
– Alok Deshwal
Nov 10 at 16:30
add a comment |
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 :
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.
javascript node.js methods mongoose find
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.
javascript node.js methods mongoose find
javascript node.js methods mongoose find
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
add a comment |
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
add a comment |
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}
});
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
add a comment |
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}
});
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
add a comment |
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}
});
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
add a comment |
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}
});
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}
});
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
try this link: stackoverflow.com/questions/7334390/…
– Alok Deshwal
Nov 10 at 16:30