Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose创建多个连接_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js Mongoose创建多个连接

Node.js Mongoose创建多个连接,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,下面我将在express app上创建多个数据库连接。当我将架构范围导入到连接时,我不能使用诸如.find | |.findOne等模型函数。。我得到了下一个错误 UserSchema.find不是一个函数 是否有我遗漏的东西需要补充,或者是我做错了什么 以下是模式代码: models/user.js const { Schema } = require('mongoose'); const UserSchema = new Schema({ name: { type: String}

下面我将在express app上创建多个数据库连接。当我将架构范围导入到连接时,我不能使用诸如.find | |.findOne等模型函数。。我得到了下一个错误

UserSchema.find不是一个函数

是否有我遗漏的东西需要补充,或者是我做错了什么

以下是模式代码:

models/user.js

const { Schema } = require('mongoose');

const UserSchema = new Schema({
    name: { type: String},
    lastname: { type: String}
});

module.exports = UserSchema;
然后,我创建了一个连接,其中的模型与它关联

数据库/连接_1.js

const mongoose = require('mongoose');
const { Keys } = require('../keys');

const conn = mongoose.createConnection(
    `mongodb://${Keys.DatabaseUser}:${Keys.DatabasePassword}@${Keys.DatabaseHost}/${Keys.DatabaseShema}`
    , { 
        useNewUrlParser: true,
        useCreateIndex: true,
        useFindAndModify: true
    });

    conn.model('User', require('../models/user'), 'Users')

module.exports = conn;
但当我尝试使用我的模式获取数据库的数据时,我得到了下一个错误:

UserSchema.find不是一个函数

routes/index.js

const app = require('express').Router();
const UserSchema = require('../models/user');

app.get('/index', async (req,res) => {
    console.log(await UserSchema.find({name: 'Jhon Doe'}))
})

module.exports = app;

这是我在创建帖子时犯的一个打字错误。错误仍然发生您无法对架构调用find,因为它是。。。模式。建立模型的蓝图。您需要导出具有所需连接的模型。