I tried to export my async function to another service from a service with this class and this function inside it.
It looks like this:
const express = require("express");
const mongoDB = require('mongodb').MongoClient;
const url = "here I paste url to my databse(everything is good here)";
class CheckService {
async isUserExists(username) {
const connection = await mongoDB.connect(url);
const query = {name: username};
const db = connection.db("users");
const result = await db.collection("users").find(query).toArray();
connection.close();
return result.length !== 0;
}
}
module.exports = new CheckService();
After this, in another service, I imported it like this:
const checkService = require('./check.service');
And I called my function like this:
console.log('function:', checkService.isUserExists(username));
After this, in the console, I have: function: Promise { pending }
What's wrong here? I don't very understand how promises, async, and await work. Can you give me something to read or watch about promises, async, await and other stuff like this? Please, help!