este es el archivo de mangosta que ejecuto a través de Node:
const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/shopApp') .then(()=>{ console.log('CONNECTION OPEN!!!') }) .catch(err=>{ console.log('HO NO ERROR!!!') console.log(err) }) // alternative syntax for Schema, now we can add additional information. the require. const productSchema = new mongoose.Schema({ name: { type: String, required: true, maxlength: 20 // Schema options }, price: { type: Number, // number character inside a string will be accepted and the opposite also!!!!!!!!!! min: [0, 'Price must be positive'] // minimum character from SchemaTypes options and then error msg }, onSale: { type: Boolean, default: false }, categories: { //[String] which means: it should be an array only consisting of strings type: [String] }, // qty: { online: { type: Number, default: 0 }, inStore: { type: Number, default: 0 } }, size: { type: String, enum: ['S', 'M', 'L'] // can be only one of this values, else it return an error } }) const product = mongoose.model('product', productSchema); //instance methods // someSchema.methods.findSimilarType = function() { //... //} productSchema.methods.greet = function(){ console.log('HELLO!!! HI!!! HOWDY!!!') } // we want to use regular function /*const bike = new product({name: 'Cycling Jersy', price: 18.50, categories: ['Cycling', ' Safety'], size: 'XL'}) // the mongo will ignore the color property bike.save() .then(data=>{ console.log('IT WORKED!') console.log(data) }) .catch(err=>{ console.log('HO NO ERROR!!!') console.log(err) })*/ product.findOneAndUpdate({name: 'Tire pump'}, {price: 1}, {new: true, runValidators: true}) // Schema constraints don't work with updating ( it below minimum price), // you need to set ---> runValidators: true!!!!! .then(data=>{ console.log('IT WORKED!') console.log(data) }) .catch(err=>{ console.log('HO NO ERROR!!!') console.log(err) })en node.js cargué este archivo mangosta y creé una instancia del modelo e intenté ejecutar la función saludar en él.
const p = new product({name: 'Big Bag', price: 10}) p.greet()En segundo plano, mongod y mongo se ejecutan en el aviso.
Recibo un mensaje 'saludar no es una función'.