【问题标题】:TypeError: POINT.find is not a function mongooseTypeError: POINT.find 不是函数 mongoose
【发布时间】:2020-08-08 18:16:04
【问题描述】:
TypeError: Point.find is not a function
    at /Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/routes/api.js:10:11
    at Layer.handle [as handle_request] (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/layer.js:95:5)
    at /Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:317:13)
    at /Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:275:10)
    at /Users/milan/MILAN/react-hand-maridian/react-hand-meridian/exp/node_modules/express/lib/router/index.js:635:15

在为 mongoose 执行 Model.find() 时遇到此错误,每个声明看起来都不错。 mongo Atlas中的数据库是point

  • Exp
    • /型号
      • point.model.js
    • /路线
      • api.js
    • server.js

point.model.js

const mongoose = require("mongoose");

const pointSchema = new mongoose.Schema({
    name: {
        type: String,
        unique: true,
        required: true,
    },
    meridianGroup: {
        type: String,
        required: true,
    },
    sideFacing: { 
        type: String, 
        required: true,
    },
    rightHand: {
        type: Object,
        offX: { 
            type: Number, 
            required: true,
        },
        offY: { 
            type: Number, 
            required: true,
        },
    },
    leftHand: {
        type: Object,
        offX: { 
            type: Number, 
            required: true,
        },
        offY: { 
            type: Number, 
            required: true,
        },
    },
});

module.export = mongoose.model("point", pointSchema);

API.js

const router = require("express").Router();
const Point = require("../models/point.model");

// This are are API routes

// Get all points
router.route('/points').get((req, res)=>{
    console.log(Point)
    // res.send("all points")
    Point.find()
        .then(points => res.json(points))
        .catch(err => res.status(400).json(err))
});

module.exports = router;

【问题讨论】:

  • 您可能导入了Point 错误,console.log(Point) 的输出是什么?
  • 输出为空{ }
  • 您没有正确导入 Point

标签: node.js mongodb express mongoose typeerror


【解决方案1】:

以错误的方式导出模型

module.exports = mongoose.model("point", pointSchema);

如果

module.export = mongoose.model("point", pointSchema);

【讨论】:

    猜你喜欢
    • 2020-12-02
    • 2015-10-28
    • 1970-01-01
    • 2017-03-09
    • 2019-02-20
    • 2018-06-14
    • 2017-04-08
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多