'greet is not a function (However It declared in mongoose file as a Schema method) (JS) -
this is the mongoose file that I run through 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)
})
in node.js I loaded this mongoose file and created an instance of the model and tried to run the function greet on it.
const p = new product({name: 'Big Bag', price: 10})
p.greet()
In the background mongod and mongo are running both on the prompt.
I get a message 'greet is not a function'.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
