Я сейчас новичок в nodeJS. Я должен создать функцию, которая должна ждать результата, и после того, как результат заполнен, он должен возвращать данные. Это моя функция
async function getReplyObject(reply_id, language, auto_translate){
var new_sql1 = 'SELECT IFNULL((SELECT CONCAT(users.firstname," ",users.lastname) as owner_name FROM users WHERE users.id = messages.owner_id), "") AS owner_name,messages.forwarded, IFNULL(message_files.file_id, "") as file_id, IFNULL(files.thumbnail, "") as thumbnail, IFNULL(files.text, "") as audio_text, IFNULL(files.text, "") as original_audio_text, IFNULL(files.file_url, "") as audio_url, IFNULL(files.file_url, "") as original_audio_url, CONCAT(users.firstname," ", users.lastname) as sender_name , GROUP_CONCAT(messages.message SEPARATOR " ") as message, messages.*, UNIX_TIMESTAMP(messages.created_at) as unix_time, GROUP_CONCAT(messages.message SEPARATOR " ") as original_message , "" as group_files,IFNULL(messages.owner_id, 0) as owner_id,IFNULL(UNIX_TIMESTAMP(messages.forwarded_at), "") as forwarded_unix FROM messages JOIN users ON messages.sender_id = users.id LEFT JOIN message_files ON messages.id = message_files.message_id LEFT JOIN files ON files.id = message_files.file_id WHERE messages.id = '+reply_id+' GROUP BY messages.group_id ORDER BY messages.id DESC';
var b_sql = 'SELECT messages.forwarded, IFNULL(message_files.file_id, "") as file_id, IFNULL(files.thumbnail, "") as thumbnail, IFNULL(files.text, "") as audio_text, IFNULL(files.text, "") as original_audio_text, IFNULL(files.file_url, "") as audio_url, IFNULL(files.file_url, "") as original_audio_url, CONCAT(users.firstname, users.lastname) as sender_name ,messages.*, UNIX_TIMESTAMP(messages.created_at) as unix_time, messages.message as original_message , "" as identifier FROM messages JOIN users ON messages.sender_id = users.id LEFT JOIN message_files ON messages.id = message_files.message_id LEFT JOIN files ON files.id = message_files.file_id WHERE messages.id = '+reply_id+' ORDER BY messages.id DESC LIMIT 20';
await connection.query(new_sql1, (err, chats) => {
if (err) {
helper.sendError(NOT_FOUND_ERROR, res, errors, req);
return;
} else {
await connection.query(b_sql, (err, result_set) => {
result_set.forEach(function (key, i) {
if (key.type == 'audio') {
key.audio_url = base_url + '/api/chat/get_file?file_id=' + key.file_id + '&lang=' + language
key.message = 'Shared an audio'
} else if (key.type == 'location') {
key.message = 'Shared a location'
} else if (key.type == 'video') {
key.message = 'Shared a video'
} else if (key.type == 'contact') {
key.message = 'Shared a contact'
} else if (key.type == 'document') {
key.message = 'Shared a document'
}
})
var msgs = [];
var indexs = [];
var new_links = [];
var audio_messages = [];
var new_data = {};
itemsProcessed = 0;
chats.forEach(function (key, i) {
key.group_files = []
result_set.forEach(function (key2, j) {
if (key2.group_id == key.group_id) {
new_links.push(key2)
key.group_files = new_links
}
})
new_links = []
if (key.message != '' || key.message != null) {
msgs.push({
"text": key.message
})
indexs.push(i)
}
if (key.type == 'audio') {
if (key.audio_text != null) {
audio_messages.push({
"text": key.audio_text
})
}
}
});
if (auto_translate) {
google.translateText(msgs, language, (json) => {
data = JSON.parse(json)
data.forEach(function (key, i) {
chats[i].message = key.translations[0].text
if (chats[i].type == 'audio' && chats[i].audio_text != '') {
chats[i].audio_url = base_url + '/api/chat/get_file?file_id=' + chats[i].file_id + '&lang=' + language;
}
if(chats[i].type == 'contact' || chats[i].type == 'document'){
chats[i].message = chats[i].original_message;
}
});
return JSON.stringify(chats[0])
});
} else {
return JSON.stringify(chats[0])
}
})
}
});
}
var reply_id = 6308;
var current_user = {
language: 'ar',
auto_translate: 1
}
var reply_obj = getReplyObject(reply_id, current_user.language, current_user.auto_translate);
console.log(reply_obj);
Но проблема в том, что я не могу получить асин c и ожидать правильной работы. Я попробовал это без asyn c и ждал, но функция возвращала неопределенную, потому что она не ждала вызовов и прочего. Что мне здесь не хватает?