galleryApi
parent
f12e2d9e0a
commit
8b420fb34f
|
@ -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;
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
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;
|
||||
|
||||
// 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);
|
||||
databasesList = client.db().iimttnewdb().listDatabases();
|
||||
console.log("Databases:");
|
||||
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
db.collection('gameData').findOne({ userId: userId, gameName: gameName, gameID: gameID }, (err, gameData) => {
|
||||
if (err) {
|
||||
console.error('Error fetching game data:', err);
|
||||
return res.status(500).json({ message: 'Internal Server Error' });
|
||||
}
|
||||
|
||||
if (gameData) {
|
||||
res.json({
|
||||
score: gameData.score.toString(),
|
||||
gameTime: gameData.gameTime.toFixed(2).toString()
|
||||
});
|
||||
} else {
|
||||
res.status(404).json({ message: 'Game data not found' });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = getGameScore;
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
const getGameScore = (req, res) => {
|
||||
const { MongoClient } = require('mongodb');
|
||||
|
||||
// Connection URI to your MongoDB instance
|
||||
const url = process.env.MONGODB_URL;
|
||||
const dbName = 'test';
|
||||
|
||||
// Create a new MongoClient
|
||||
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
let db;
|
||||
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
|
||||
.then(client => {
|
||||
console.log('Connected to Database',dbName);
|
||||
db = client.db(dbName);
|
||||
})
|
||||
.catch(error => console.error(error));
|
||||
// Function to list all databases
|
||||
async function listDatabases() {
|
||||
try {
|
||||
const { userId, gameName, gameID } = req.body;
|
||||
const gameData = await db.collection('gameData').findOne({
|
||||
userId: userId,
|
||||
gameName: gameName,
|
||||
gameID: gameID
|
||||
});
|
||||
|
||||
if (gameData) {
|
||||
res.json({
|
||||
score: gameData.score.toString(),
|
||||
gameTime: gameData.gameTime.toFixed(2).toString()
|
||||
});
|
||||
} else {
|
||||
res.status(404).json({ message: 'Game data not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Internal Server Error' });
|
||||
console.error(error);
|
||||
} finally {
|
||||
// Ensure that the client will close when you finish
|
||||
await client.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Call the function
|
||||
listDatabases();
|
||||
|
||||
};
|
||||
|
||||
module.exports = getGameScore;
|
||||
|
Loading…
Reference in New Issue