52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
const getGameScore = (req, res) => {
|
|
const { MongoClient } = require('mongodb');
|
|
|
|
async function main() {
|
|
const url = process.env.MONGODB_URL;
|
|
const dbName = process.env.MONGO_DB_NAME;
|
|
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });
|
|
|
|
try {
|
|
// Connect to the MongoDB client
|
|
await client.connect();
|
|
|
|
// Select the database and collection
|
|
const database = client.db(dbName); // 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' });
|
|
|
|
const { userId, gameName, gameID } = req.body;
|
|
const gameData = await collection.findOne({
|
|
childId: userId,
|
|
gameName: gameName,
|
|
gameID: gameID
|
|
});
|
|
|
|
if (gameData) {
|
|
res.json({gameData});
|
|
} else {
|
|
res.status(404).json({ message: 'Game data not found' });
|
|
}
|
|
|
|
// 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;
|
|
|