简体   繁体   English

类型错误:_models_Products__WEBPACK_IMPORTED_MODULE_1__.default.find 不是函数

[英]TypeError: _models_Products__WEBPACK_IMPORTED_MODULE_1__.default.find is not a function

I am making Next Js web application and browesr is showing type error in 7th line in first block of code .The error is under Product.find()我正在制作 Next Js Web 应用程序,browesr 在第一个代码块的第 7 行显示类型错误。错误在 Product.find() 下
TypeError: models_Products__WEBPACK_IMPORTED_MODULE_1 _.default.find is not a function类型错误: models_Products__WEBPACK_IMPORTED_MODULE_1 _.default.find 不是函数

   6 | const handler = async (req, res) => {
>  7 |   let products = await Product.find();
     |                       ^
   8 |  
   9 |   res.status(200).json({ products });
  10 | };

**This is my database of mongodb ** **这是我的mongodb数据库**



import mongoose from "mongoose";

const connectDb = (handler) => async (req, res) => {
  if (mongoose.connections[0].readyState) {
    return handler(req, res);
  }
  await mongoose.connect(process.env.MONGO_URI);
  return handler(req, res);
};
export default connectDb;

`

This is connection of my database to next js app in api folder这是我的数据库到 api 文件夹中下一个 js 应用程序的连接



import connectDb from "../../database";
import Product from "../../models/Products";

const handler = async (req, res) => {
  let products = await Product.find();

  res.status(200).json({ products });
};

export default connectDb(handler);

* This is my product schema *这是我的产品架构



const mongoose=require("mongoose")

const productSchema=new mongoose.Schema({

    title:{
        type:String,
        required:true
    },
    slug:{
        type:String, required:true
    },
    desc:{type:String,required:true},
    img:{type:String,reuired:true},
    category:{type:String,reuired:true},
    size:{type:String},
    color:{type:String},
    price:{type:Number},
    availableQty:{type:String,required:true}
   

},{timestamps:true});

mongoose.models={}

export default mongoose.model=('Product',productSchema)

Try to export your model like this:尝试像这样导出model

export default mongoose.models?.Product || mongoose.model('Product', productSchema);

You may remove the mongoose.models = {} line.您可以删除mongoose.models = {}行。

暂无
暂无

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

相关问题 未捕获的类型错误:_models_Search__WEBPACK_IMPORTED_MODULE_0___default.a 不是构造函数 - Uncaught TypeError: _models_Search__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor TypeError:react__WEBPACK_IMPORTED_MODULE_6 ___ default.a.useState不是一个函数 - TypeError: react__WEBPACK_IMPORTED_MODULE_6___default.a.useState is not a function 类型错误:moment__WEBPACK_IMPORTED_MODULE_3___default(...)(...).calendar(...).sort 不是函数 - TypeError: moment__WEBPACK_IMPORTED_MODULE_3___default(...)(...).calendar(...).sort is not a function 类型错误:标记为__WEBPACK_IMPORTED_MODULE_3___default(...) 不是函数 - TypeError: marked__WEBPACK_IMPORTED_MODULE_3___default(...) is not a function "类型错误:_fire__WEBPACK_IMPORTED_MODULE_1__.default.auth 不是函数" - TypeError: _fire__WEBPACK_IMPORTED_MODULE_1__.default.auth is not a function 类型错误:_fire__WEBPACK_IMPORTED_MODULE_11__.default.collection 不是 function - TypeError: _fire__WEBPACK_IMPORTED_MODULE_11__.default.collection is not a function 类型错误:_firebase__WEBPACK_IMPORTED_MODULE_9__.default.collection 不是 function - TypeError: _firebase__WEBPACK_IMPORTED_MODULE_9__.default.collection is not a function 类型错误:firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.analytics 不是 function - TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.analytics is not a function 类型错误:Webpack 导入的模块不是 function - TypeError: Webpack imported module is not a function TypeError:__ WWEPACK_IMPORTED_MODULE_0_react ___ default.a.createRef不是函数 - TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createRef is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM