Я изо всех сил пытаюсь получить значение из вызова GET PouchDB, чтобы я мог использовать его в своем HTML во время начальной загрузки страницы.Мне удалось создать базу данных и сохранить запись, что достаточно просто, поскольку мне не нужно ждать возвращаемого значения.
Используя инструменты разработчика Chrome, я могу установить точку останова в вызове get PouchDB и увидеть, чтоон возвращает нужные мне данные, но получает это значение вне вызова GET aysnc, с которым у меня возникают проблемы.
Соответствующий код выглядит следующим образом (ОБНОВЛЕНИЕ для ответа IanC):
jQuery(document).ready(function () {
// Store note data as object for pass-by-reference
var notesData = {note_text:null, note_rev_id:null};
// Create/get database
var notesDb = null;
try {
notesDb = new PouchDB(notesDbName);
} catch (e) {
console.log(e);
}
// Query for a note entry that has the matching ID
function pouchDbGetNote(dbId, dbObj, noteObj){
dbObj.get(dbId)
.then(function (response) {
returnNoteData(response, noteObj);
}).catch(function (err) {
console.log(err);
});
}
// Return the result of the GET query to a variable that is is
// accessible outside of the aysnc pouchDB GET query.
function returnNoteData(result, noteObj){
// Store the note text in local scope - Used in UI display with Div tag
noteObj.note_text = result.note;
// Store the note revision id in local scope (Need for updating note in db)
noteObj.note_rev_id = result._rev;
}
/*
This gets called and debugging thread moves onto lines below
immediately (notesData is empty at this point) then 'returnNoteData'
gets called a second time but by this time the UI update calls have occurred.
*/
pouchDbGetNote(dbEntryId, notesDb, notesData);
// TODO: Insert the note text into the UI Div, update data attribute on btn with revision id
notesContentInitialValue = notesData.note_text;
console.log('msg@ Init - Note: ' + notesData.note_text);
});