Search code examples
node.jsmongodbapiexpresses6-promise

I tried to get data from mongodb atlas


I'm using this to connect mongodb. In the previous i've run it perfectly but after i close the terminal and run it a gain then i got error.

+this is the my code

async function loadTasksCollection(){
    const url="mongodb+srv://user:[email protected]/mytasklist?retryWrites=true&w=majority";
    const client=new MongoClient(url,{useNewUrlParser:true,useUnifiedTopology: true});
    try {
        await client.connect();
        console.log("sucess")
    } catch (error) {
        console.log(error);
        
    }
    return client.db("tasklist").collection("task");
    
    
}

// to read the task
router.get('/',async(req,res)=>{  
    const task= await loadTasksCollection()
    res.send(await task.find({}).toArray())
    
});

+this is the first the erorr

    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(3) {
      'mytasklist-shard-00-02.a75ix.mongodb.net:27017' => [ServerDescription],
      'mytasklist-shard-00-00.a75ix.mongodb.net:27017' => [ServerDescription],
      'mytasklist-shard-00-01.a75ix.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

+after above a few second i got this

(node:6213) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6213) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Solution

  • I've found the solution because i close my computer and open it again my IP address was also change but i forgot to change the network access IP address in Mongodb atlas. as this is just a pratice so Now i change Network access IP address to public so i never face that problem again enter image description here