Async
function is returning [object Promise]
instead of the desired real value. Interestingly, I can see the value in the console log.
It seems like this behavior is expected from the function, but I'm unsure how to fix my code.
This code snippet is related to vue.js, using electron-vue and NeDB.
<template>
<div>
{{ testNedb3('NDId6sekw6VYLmnc') //this is a test by adding specific id }}
</div>
</template>
<script>
import Promise from 'bluebird'
export default {
methods: {
dbFindAsync2: function (db, opt) {
return new Promise(function (resolve, reject) {
db.find(opt, function (err, doc) {
if (err) {
reject(err)
} else {
resolve(doc)
}
})
})
},
testNedb3: async function (id) {
const flattenMemAsync = function (arr) {
return new Promise(function (resolve) {
Array.prototype.concat.apply(
[],
arr.map(function (arr) {
resolve(arr.members)
})
)
})
}
const filterByNameIdAsnc = function (arr) {
return new Promise(function (resolve) {
const result = arr.filter(function (member) {
return member.nameId === id
})
resolve(result)
})
}
this.dbFindAsync2(
this.$db, { 'members.nameId': id }, { 'members': 1, _id: 0 }
).then(function (res) {
const docs = res
flattenMemAsync(docs).then(function (res) {
const flatMembers = res
filterByNameIdAsnc(flatMembers).then(function (res) {
console.log('result', res)
console.log('result_firstname', res[0].firstName)
return res
})
})
})
},
this.$db
fetches data from NeDB in the form of a two-dimensional array. To make it more manageable, I am trying to flatten the array with flattenMemAsync
and filter out unwanted data with filterByNameIdAsnc
.
The output of console.log('result', res)
is an array, while
console.log('result_firstname', res[0].firstName)
returns a string.
I attempted changing the calling code from
{{ testNedb3('NDId6sekw6VYLmnc') }}
to {{ {{ testNedb3('NDId6sekw6VYLmnc').then(value => {return value}) }}
, but the outcome remained unchanged.
I also tried
{{ await testNedb3('NDId6sekw6VYLmnc').then(value => {return value}) }}
, resulting in an error message stating "Parsing error: Cannot use keyword 'await' outside an async function."
Any suggestions would be greatly appreciated. Thank you.