I’ve tried several methods to use the mongodb insert methods, but it seems tobe non functional:
The code:
Bellow Code didn't work
`const dbConnect = require('./mongodb');
const insertMany = async () => {
try {
console.log("insertMany function initiated");
const db = await dbConnect();
console.log('DB Connection details are as follows:n');
console.log(db);
const result = await db.insertMany({
name: 'Asus Expert Book P2',
brand: 'Asus',
price: 1600,
category: 'Notebooks'
},{
name: 'DELL XPS 13 Developer Edition',
brand: 'Dell',
price: 1800,
category: 'Notebooks'
},{
name: 'System 76 16"',
brand: 'System 76',
price: 2100,
category: 'Notebooks'
},{
name: 'Apple Macbook Pro Max 16"',
brand: 'Apple',
price: 1600,
category: 'Notebooks'
});
return result; Return the result of the insertManyion
} catch (error) {
console.error('Error insertManying data:', error);
throw error; Re-throw the error to handle it outside
}
}
module.exports = insertMany;
Call the function elsewhere
Example:
const insertManyFunction = require('./path/to/insertManyFunction');
insertManyFunction().then(result => console.log('insertManyion result:', result)).catch(error => console.error('insertManyion error:', error));`
————-failed execution—————-
` const dbConnect = require(‘./mongodb’);
const insertMany = async () => {
console.log("insertMany function initiated");
const db = await dbConnect();
console.log('DB Connection details are as follows:n');
console.log(db);
const result = await db.insertMany({
name: 'Asus Expert Book P2',
brand: 'Asus',
price: 1600,
category: 'Notebooks'
},{
name: 'DELL XPS 13 Developer Edition',
brand: 'Dell',
price: 1800,
category: 'Notebooks'
},{
name: 'System 76 16"',
brand: 'System 76',
price: 2100,
category: 'Notebooks'
},{
name: 'Apple Macbook Pro Max 16"',
brand: 'Apple',
price: 1600,
category: 'Notebooks'
});
return result; Return the result of the insertManyion
}
module.exports = insertMany;`
`const dbConnect = require('./mongodb');
const insertMany = async () => {
console.log("insertMany function initiated");
const db = await dbConnect();
console.log('DB Connection details are as follows:n');
console.log(db);
return db.insertMany({
documents: [
{
name: 'Asus Expert Book P2',
brand: 'Asus',
price: 1600,
category: 'Notebooks'
},
{
name: 'DELL XPS 13 Developer Edition',
brand: 'Dell',
price: 1800,
category: 'Notebooks'
},
{
name: 'System 76 16"',
brand: 'System 76',
price: 2100,
category: 'Notebooks'
},
{
name: 'Apple Macbook Pro Max 16"',
brand: 'Apple',
price: 1600,
category: 'Notebooks'
}
]
});
}
module.exports = insertMany;`
I need to know what’s going wrong here.. please help urgent.
I am expecting the code to be updating to following data base configuration:
“`
const {MongoClient}= require ('mongodb');
const url = 'mongodb:127.0.0.1:27017/e-comm';
const client = new MongoClient(url);
const database='e-comm';
async function dbConnect(){
console.log('Database connection is initiated')
let result= await client.connect();
let db=result.db(database);
return db.collection('products');
let response= await collection.find({}).toArray()
console.log(response);
}
dbConnect().then((resp)=>{
resp.find().toArray().then((data)=>{
console.warn(data)
})
})
console.warn(dbConnect());
const main = async ()=>{
let data = await dbConnect();
data = await data.find().toArray();
console.log(data);
}
main();
const url = 'mongodb:127.0.0.1:27017/e-comm';
const client = new MongoClient(url);
const database='e-comm';
async function dbConnect(){
let result= await client.connect();
let db=result.db(database);
return db.collection('products');
let response= await collection.find({}).toArray()
console.log(response);
}
module.exports=dbConnect;
```
Here, commented code seems to be working well hence the DB is connected and fetch the response from database.