简体   繁体   中英

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

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.

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}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM