简体   繁体   English

我不明白如何在猫鼬中使用 findOrCreate 方法

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

Following this Schema cap.model.js :遵循这个架构 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 :我想使用以下数据从 get('/url/:param1') 检索数据:

  1. Find the documents for which the the list contains param1 and return their titles in an array.查找列表中包含 param1 的文档并在数组中返回它们的标题。

  2. Find the document which title is param1, and return its list.找到标题为 param1 的文档,并返回其列表。 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.然后我想要 console.log :一侧是标题 (2),另一侧是由标题数组 (1) 和列表 (2) 形成的数组。

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.我的问题是我找不到或不理解 findOrCreate ( https://www.npmjs.com/package/mongoose-findorcreate ) 方法以及我必须使用哪些参数。

My code looks like this for the moment :我的代码目前看起来像这样:

In route.js :在 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:在 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.在您的cap.controller.js 中有很多错误。 Firstly why the foc method is outside the cap_get one?首先为什么foc方法在cap_get之外? And secondly you never call the foc method, why?其次,你从不调用foc方法,为什么?

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.在您的情况下,您想检查是否存在由参数给出的标题的Cap ,如果不是这种情况,则创建它。

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. cap_get只是一个带有(req, res, next)签名的快速回调,而findOrCreate第一个参数是主键,所以这里如果这个键(标题)不存在大写,它将创建它,否则它会返回文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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