mongoose 常量错误= this.已关闭?新错误_1.PoolClosedError(this):新错误_1.池已清 debugging 误(此);

gajydyqb  于 2023-03-18  发布在  Go
关注(0)|答案(1)|浏览(204)

我试着把数据保存到我的mongodb本地服务器,但是它抛出了这个错误-

C:\Users\VIKRAM\node_modules\mongodb\lib\cmap\connection_pool.js:476

    const error = this.closed ? new errors_1.PoolClosedError(this) : new errors_1.PoolClearedError(this);
                                ^

PoolClosedError [MongoPoolClosedError]: Attempted to check out a connection from closed connection pool
    at ConnectionPool.processWaitQueue (C:\Users\VIKRAM\node_modules\mongodb\lib\cmap\connection_pool.js:476:45)
    at ConnectionPool.close (C:\Users\VIKRAM\node_modules\mongodb\lib\cmap\connection_pool.js:280:14)
    at Server.destroy (C:\Users\VIKRAM\node_modules\mongodb\lib\sdam\server.js:126:21)
    at destroyServer (C:\Users\VIKRAM\node_modules\mongodb\lib\sdam\topology.js:424:12)
    at node:internal/util:364:7
    at new Promise (<anonymous>)
    at destroyServer (node:internal/util:350:12)
    at C:\Users\VIKRAM\node_modules\mongodb\lib\sdam\topology.js:211:56
    at Function.from (<anonymous>)
    at Topology.close (C:\Users\VIKRAM\node_modules\mongodb\lib\sdam\topology.js:210:40) {
  address: '127.0.0.1:27017',
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.13.0

代码在这里-

const { MongoClient } = require('mongodb');
const { Collection, connection } = require('mongoose');
async function main(){
    const url = "mongodb://127.0.0.1:27017/mydb"
    const client = new MongoClient(url);
    
    try{
        await client.connect();
        console.log('connection connected..')
        // await createcollectionname(client);
        createListingbases(client,{
            name: "vikram is best",
            summmary : "just another day of coding",
            bedroom: "yes i am single",
            bathrooom: "no comments :|"
        })
    } catch(e){
        console.error(e);
    } finally {
        await client.close()
    }
}

main().catch(console.error)

async function createListingbases(client,newListing){
    const result = await client.db("sample_ddb").collection("listing and reviews").insertOne(newListing)
    console.log(`new listing createdd with the following id : ${result.insertedId}`);
        
    
}

我想把这些数据保存到我的mongodb服务器上,但是代码不起作用,我尝试了很多其他的方法,但是都不起作用。

nkhmeac6

nkhmeac61#

函数“createListingbases()”需要等待。客户端在实现连接承诺时关闭。如果您注解等待客户端.close(),则可以检查它是否工作。

const { MongoClient } = require('mongodb');
// const { Collection, connection } = require('mongoose');
async function main(){
const url = "mongodb://127.0.0.1:27017"
const client = new MongoClient(url);

try{
    await client.connect();
    console.log('connection connected..')
    // await createcollectionname(client);
    await createListingbases(client,{
        name: "vikram is best",
        summmary : "just another day of coding",
        bedroom: "yes i am single",
        bathrooom: "no comments :|"
    })
} catch(e){
    console.error(e);
} finally {
    await client.close()
}
}

main().catch(console.error)

async function createListingbases(client,newListing){
const result = await client.db("sample_ddb").collection("listing and 
reviews").insertOne(newListing)
console.log(`new listing createdd with the following id : 
${result.insertedId}`);
    

}

相关问题