refactor: use findOne instead of find

This commit is contained in:
2022-10-07 15:40:33 +02:00
parent a526f5ea6b
commit 8ea72518d0
2 changed files with 23 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ dotenv.config()
import sernTime from "./schemas/sern-time.js"
import bodyParser from "body-parser"
import rateLimit from "express-rate-limit"
import { consolelogTime } from "./util/consolelogTime.js"
app.use(bodyParser.json())
app.disable("x-powered-by")
const limiter = rateLimit({
@@ -19,7 +20,7 @@ app.use(limiter)
const englishRegex = /^[A-Za-z0-9]*$/
await mongoose.connect(`${process.env.MONGODB}`).then(() => {
console.log("Connected to MongoDB!")
consolelogTime(`Connected to MongoDB!`)
})
app.post("/sern/newTime", async (req, res, next) => {
@@ -64,20 +65,19 @@ app.post("/sern/newTime", async (req, res, next) => {
app.get("/sern/availableTime", async (req, res, next) => {
let get = (await sernTime.find()) as any
get = get.map((data) => data.name)
get = get.map(data => data.name)
res.send(get)
})
app.get("/sern/getTime", async (req, res, next) => {
if (req.query.name) {
let get = (await sernTime.find({ name: req.query.name })) as any
get = get.map((data) => data.timezone)
res.send(get)
const get = await sernTime.findOne({ name: req.query.name })
res.send(get!.timezone)
} else {
res.json({ "error": "Option name not provided." })
}
})
app.listen(7272, () => {
console.log(`listening`)
consolelogTime(`listening`)
})

17
util/consolelogTime.ts Normal file
View File

@@ -0,0 +1,17 @@
export async function consolelogTime(message: string) {
let unix_timestamp = Date.now()
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp * 1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
const convertedTime = hours + ':' + minutes.substring(-2) + ':' + seconds.substring(-2);
return console.log(`[${convertedTime}] ${message}`);
}