无法读取属性'查找'未定义的mongodb

发布于 2025-02-12 06:13:32 字数 1494 浏览 0 评论 0原文

我开始在nodejs项目中实现猫鼬。我在集合中创建了一个测试记录来测试背面的CRUD操作,我正在尝试测试Mongo的Find()属性,但我不确定该如何做。

这是我与Mongoose的联系:

const mongoose = require('mongoose');

const mongoURI: string = "mongodb://localhost:27017"
const mongoDB: string = "testdb"

export const setMongo = async() => {

  try {

    let mongodbURI: string = `${mongoURI}/${mongoDB}`
    await mongoose.connect(mongodbURI);
    
    console.log('conected DB')
    
  } catch (error) {
    console.log('error DB')
  }

};

这是我的架构:

const mongoose = require('mongoose');

const companiesSchema = new mongoose.Schema ({
    name: {
        type: String,
        required: true
    },
    phoneNumber: {
        type: Number,
        required: true,
        unique: true
    }
}, {
    versionKey: false,
    collection: 'companies'
});

module.exports = mongoose.model('Companies', companiesSchema);

我的呼刻

const companySchema = require("../../schemas/companies")
const db = companySchema.Companies

export class Repository {

    public async getAll(): Promise<any> {        
        try {

            console.log('getAll()')
            const comp = await db.find({});
            console.log(comp)
            
        } catch (error) {
            console.log(error)
        }
    }

}

这是

TypeError: Cannot read property 'find' of undefined

Update

如何使用Find()方法获取总数据?是否可以?

I am starting to implement mongoose in a nodejs project. I have created a test record in a collection to test the CRUD operations from the back, I am trying to test the find() property of mongo but I am not sure how to do it.

This is my connection to mongoose:

const mongoose = require('mongoose');

const mongoURI: string = "mongodb://localhost:27017"
const mongoDB: string = "testdb"

export const setMongo = async() => {

  try {

    let mongodbURI: string = `${mongoURI}/${mongoDB}`
    await mongoose.connect(mongodbURI);
    
    console.log('conected DB')
    
  } catch (error) {
    console.log('error DB')
  }

};

This is my Schema:

const mongoose = require('mongoose');

const companiesSchema = new mongoose.Schema ({
    name: {
        type: String,
        required: true
    },
    phoneNumber: {
        type: Number,
        required: true,
        unique: true
    }
}, {
    versionKey: false,
    collection: 'companies'
});

module.exports = mongoose.model('Companies', companiesSchema);

This is my resposity.ts:

const companySchema = require("../../schemas/companies")
const db = companySchema.Companies

export class Repository {

    public async getAll(): Promise<any> {        
        try {

            console.log('getAll()')
            const comp = await db.find({});
            console.log(comp)
            
        } catch (error) {
            console.log(error)
        }
    }

}

This is the error it shows:

TypeError: Cannot read property 'find' of undefined

How should I create the connections or queries to mongo?

UPDATE

How can I get the total of the data with the find() method? Is it possible?

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

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

发布评论

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

评论(1

猫九 2025-02-19 06:13:36

您只需在控制器中导入模型,然后就可以使用查询:

const Companies = require("../../schemas/companies")

export class Repository {

    public async getAll(): Promise<any> {        
        try {

            console.log('getAll()')
            const comp = await Companies.find({});
            console.log(comp)
            
        } catch (error) {
            console.log(error)
        }
    }

}

为了获得结果,您可以在查询后使用.count()来计算结果:

const comp = await Companies.find({}).count();

you just import your model in your controller and then you can use your query like:

const Companies = require("../../schemas/companies")

export class Repository {

    public async getAll(): Promise<any> {        
        try {

            console.log('getAll()')
            const comp = await Companies.find({});
            console.log(comp)
            
        } catch (error) {
            console.log(error)
        }
    }

}

and for get count of your result you can use .count() after your query to count your result :

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