静态不是一个函数 - 猫鼬

发布于 2025-01-10 03:20:33 字数 1382 浏览 0 评论 0原文

我收到错误“product.try() is not a function”

如果我立即调用 try() 它工作正常, 节点识别模型“产品”。

关于猫鼬的工作原理,有什么我不明白的吗? 我在一个文件中运行整个代码,并按照我参加的课程中视频中的导师步骤进行操作。

问题是什么?谢谢。

文件:product.js =>

 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)
    })
    
 
    const productSchema = new mongoose.Schema({
        name: {
            type: String,
            required: true,
            maxlength: 20   // Schema options
        },
        price: {
            type: Number, 
            min: [0, 'Price must be positive']
        },
        onSale: {
            type: Boolean,
            default: false
        },
        categories: {
        type: [String]
        }, 
        qty: {
            online: {
                type: Number,
                default: 0
            },
            inStore: {
                type: Number,
                default: 0
            }
        },
        size: {
            type: String,
            enum: ['S', 'M', 'L'] 
        }
    })
    
    const product = mongoose.model('product', productSchema); 
    
    productSchema.statics.try = function(){
        console.log('OK')
    };
    
    product.try();

I get the error "product.try() is not a function"

If I immediately invoke the try() it works ok,
and node recognize the model "product".

is there something I d'ont understand about how mongoose works?
I run the whole code in the one file and I followed the steps of the tutor from the video in the course I take.

what is the problem? thank you.

file: product.js =>

 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)
    })
    
 
    const productSchema = new mongoose.Schema({
        name: {
            type: String,
            required: true,
            maxlength: 20   // Schema options
        },
        price: {
            type: Number, 
            min: [0, 'Price must be positive']
        },
        onSale: {
            type: Boolean,
            default: false
        },
        categories: {
        type: [String]
        }, 
        qty: {
            online: {
                type: Number,
                default: 0
            },
            inStore: {
                type: Number,
                default: 0
            }
        },
        size: {
            type: String,
            enum: ['S', 'M', 'L'] 
        }
    })
    
    const product = mongoose.model('product', productSchema); 
    
    productSchema.statics.try = function(){
        console.log('OK')
    };
    
    product.try();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2025-01-17 03:20:33

您实际上需要在调用 mongoose.model() 之前添加 .try 函数,

这应该适合您:

// connect to mongose...
// ... 

const productSchema = new mongoose.Schema({ /* .. your schema ..  */})
 
// add the function to the schema before creating the model!
productSchema.statics.try = function () {
  console.log('OK')
};


// since everything is added create the product model!
const product = mongoose.model('product', productSchema);

product.try();  // should work now.

注意:.model() 函数会复制架构。在调用 .model() 之前,请确保您已经添加了想要架构的所有内容,包括挂钩!

文档

You actually need tadd the .try function before calling mongoose.model()

This should work for you:

// connect to mongose...
// ... 

const productSchema = new mongoose.Schema({ /* .. your schema ..  */})
 
// add the function to the schema before creating the model!
productSchema.statics.try = function () {
  console.log('OK')
};


// since everything is added create the product model!
const product = mongoose.model('product', productSchema);

product.try();  // should work now.

Note: The .model() function makes a copy of schema. Make sure that you've added everything you want to schema, including hooks, before calling .model()!

docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文