M
M
mrsakura522021-11-06 21:14:55
JavaScript
mrsakura52, 2021-11-06 21:14:55

async function not working. NodeJS. What to do?

I use Node.js and MongoDB. I have vk.js which is responsible for the work of the VK bot. After writing the message, a TypeError is thrown: db.newUserRegister is not a function. There is also a core that includes just the same vk.js and database.js The newUserRegister function does not work. config.js is also there and it works fine.

vk.js:

const {VK} = require('vk-io')
const config = require('../config/config.json')
const db = require('./MongoDB/database.js')

console.log(config.prefix + 'Модуль VK запущен')

const vk = new VK ({
    token: config.token,
    apiVersion: '5.131'
})

vk.updates.on('message', async function(message) {
    if (message.text == null || message.isGroup == true || message.text == 'undefined' || message.isOutbox) return;
    if (message.senderId == config.admins) {
        console.log('Сообщение от администратора! ' + message.text)
        exports.message = message.text;
        await db.newUserRegister(); //вот это выдает ошибку
        return;
    } else {
        
    }
}) /* vk updates message */

async function initialize() {
    await vk.updates.startPolling();
};

module.exports = {
    vk: vk,
    initialize: initialize
};


database.js:

const {MongoClient} = require('mongodb')
const vk = require('../vk.js')
const config = require('../../config/config.json')

const client = new MongoClient(config.db)

async function initialize() {
    try {
        await client.connect()
        console.log(config.prefix + 'Соединение установлено')
    } catch (e) {
        console.log(e)
    }
};

async function newUserRegister() {
    const users = client.db().collection('users')
    await users.insertOne({name: vk.message})
    console.log("Сообщение " + vk.message + " успешно добавлено в коллекцию users")
};

module.exports = {
    initialize: initialize,
    newUserRegister: newUserRegister
}


app.js:

const db = require('./lib/MongoDB/database.js')
const vk = require('./lib/vk.js')
const config = require('C:/Users/User/Desktop/MongoDBBot/config/config.json')

async function main() {
    await vk.initialize()
    console.log(config.prefix + 'Отслеживание началось')
    await db.initialize()
    console.log(config.prefix + 'База данных успешно запущена')
}
main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-11-06
@mrsakura52

You seem to have circular references: vk.js requests database.js and vice versa. In such cases, whoever got up first and slippers.
Take out the general functionality in a separate file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question