t
This commit is contained in:
@@ -1,28 +1,38 @@
|
||||
const getGameScore = (req, res) => {
|
||||
const { MongoClient, ObjectId } = require('mongodb');
|
||||
const { userId, gameName, gameID } = req.body;
|
||||
const url = process.env.MONGODB_URL;
|
||||
const dbName = process.env.MONGO_DB_NAME;
|
||||
let db;
|
||||
const { MongoClient } = require('mongodb');
|
||||
|
||||
// Connect to MongoDB
|
||||
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
|
||||
if (err) {
|
||||
console.error('Failed to connect to the database');
|
||||
throw err;
|
||||
}
|
||||
console.log('Connected to Database2',dbName);
|
||||
db = client.db(dbName);
|
||||
});
|
||||
db.collection('gameData').find({}).toArray((err, results) => {
|
||||
if (err) {
|
||||
console.error('Error fetching game data:', err);
|
||||
return res.status(500).json({ message: 'Internal Server Error' });
|
||||
}
|
||||
async function main() {
|
||||
const uri = 'mongodb://mongoadmin:Bijaynagardwd@10.0.0.90:27017'; // Replace with your MongoDB connection string
|
||||
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
|
||||
res.json(results);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = getGameScore;
|
||||
try {
|
||||
// Connect to the MongoDB client
|
||||
await client.connect();
|
||||
|
||||
// Select the database and collection
|
||||
const database = client.db('iimttnewdb'); // Replace with your database name
|
||||
const collection = database.collection('gameData'); // Replace with your collection name
|
||||
|
||||
// Find a single document
|
||||
const user = await collection.findOne({ userId: 'dsfdfgffgfgeg' });
|
||||
|
||||
// Output the result
|
||||
if (user) {
|
||||
console.log('User found:', user);
|
||||
} else {
|
||||
console.log('No user found with the username "john_doe"');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error connecting to MongoDB or performing query:', error);
|
||||
} finally {
|
||||
// Close the connection
|
||||
await client.close();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
|
||||
};
|
||||
|
||||
module.exports = getGameScore;
|
||||
|
||||
Reference in New Issue
Block a user