Плагины могут не регистрироваться в mongoose.plugin(myMongoosePlugin)
, поскольку модели mongoose создаются до того, как вы регистрируете плагины по всему миру.
expressjs
маршруты: Убедитесь, что в вашем app.js (server.js)
вы регистрируете плагины mongoose, прежде чем регистрируете / создаете маршруты expressjs
(которые используют монгонговые модели для связи с базой данных).
Пример:
в app.js
const express = require(express);
const mongoose = require('mongoose');
const myMongoosePlugin = require('<Mongoose Plugin file path>');
mongoose.plugin(myMongoosePlugin);
let app = express();
//register expressjs routes
require('<Express routes file path>')(app, express.Router());
// or create expressjs routes
app.post('/person', (req, res, next) => {
//where someMethod is using person mongoose model
this.someController.someMethod(someArguments)
.then((user) => {
res.json(user);
}).catch((error) => {
next(error);
});
});
// ... Some other code ...
mongoose.connect(<databaseConnectionString>);
app.listen(<Port>);
Вот фрагмент кода, который вычисляет стоимость такси (вы, вероятно, видели, что формула для его расчета равна 5 + 3*(nb_km-1)
, если nb_km строго положительно.
Итак, у вас есть 3 случая, чтобы выделить:
5 + 3*(nb_km-1)
Что дает этот маленький кусочек кода:
#include <stdlib.h>
#include <stdio.h>
#define FIRST_KM_PRICE 5
#define NEXT_KM_PRICE 3
int main() {
int nb_km, price; // declare an integer variable called nb_km, and one called price
printf("enter the number of kilometers\n");
scanf("%d", &nb_km); // get a number for the standart input (the console), and store it in nb_km
if (nb_km < 0) {
printf("Error, must be a positive number of kilometers.\nExiting ...");
exit(1);
} else {
if(nb_km == 0) {
price = 0;
} else {
price = FIRST_KM_PRICE + (nb_km - 1) * NEXT_KM_PRICE; // we calculate the price
}
printf("Price is %d dollars.\n", price);
}
return 0;
}