Here is my arrow function I am storing in this let s arrow function,
which I stored inside of a loop and merged all object in object.assign. When I run the let s arrow function, however, I'm getting a Promise { undefined } error.
let s = async() => {
Object.values(sqlQuery).map(async(o:any) => {
a.map(async(k:any) => {
c.map(async(l:any) => {
var district:any = `select slug from foa_section_content where foa_section_content_id IN (${k})`;
var [district1]: any = await connection.execute(district);
var commune:any = `select slug from foa_section_content where foa_section_content_id IN (${l})`;
var [commune1]: any = await connection.execute(commune);
s = await (Object.assign(o,{district1},{commune1}))
})
})
})
}
return (s());
The issue is that you are not returning a value from the arrow function. The syntax for an arrow function is:
let s = async() => {
return Object.values(sqlQuery).map(async(o:any) => {
a.map(async(k:any) => {
c.map(async(l:any) => {
var district:any = `select slug from foa_section_content where foa_section_content_id IN (${k})`;
var [district1]: any = await connection.execute(district);
var commune:any = `select slug from foa_section_content where foa_section_content_id IN (${l})`;
var [commune1]: any = await connection.execute(commune);
return Object.assign(o,{district1},{commune1});
})
})
});
}
Related
How can I pass the returned coin object from the displayCurrencies function to the getCoinId function to use it as a parameter in the API call for retrieving the specific coin data?
this is the function i created to return the value:
let returnID = (value) => {
return value;
};
this is the function that i want to return coin from:
let displayCurrencies = async () => {
let coinsContainer = document.querySelector(`.coins`);
try {
let coins = await getData();
let coinsArray = [];
let coinElement;
for (const coin of coins) {
coinElement = coin;
if (coinsArray.length > 20) {
break;
}
coinsArray.push(coin);
// create Nodes
let coinDisplay = createElement(`li`, `coin`);
let coinSymbolElement = createElement(`p`, `coinSymbol`);
let coinIDElement = createElement(`p`, `coinID`);
// set Values
coinSymbolElement.innerHTML = coin.symbol;
coinIDElement.innerHTML = coin.id;
// append
coinDisplay.append(coinSymbolElement, coinIDElement);
coinsContainer.appendChild(coinDisplay);
coinDisplay.addEventListener(`click`, () => {
openModal();
returnID(coin);
});
}
let returnCoin = returnID
coinDisplay.addEventListener(`click`, () => {
console.log(returnCoin);
});
console.log(returnCoin);
} catch (error) {
console.log(error);
}
};
and last, this is the function that i want to use the returned value at:
displayCurrencies();
let getCoinId = async () => {
let coinID = await displayCurrencies();
let currencyData = `https://api.coingecko.com/api/v3/coins/${coinID}`;
let responseData = await fetch(currencyData);
let dataOfCoins = await responseData.json();
console.log(dataOfCoins);
};
You can simply add an onclick event for each element, and when the call-back is called you invoke the getCoinID function passing the coinID as a parameter.
A simple example:
<ul class="coins">
</ul>
<script>
function addOnClick() {
let coinsContainer = document.querySelector('.coins');
let coins = [
{ id: 1, symbol: 'bitcoin' },
{ id: 3, symbol: 'brazil-fan-token' },
{ id: 4, symbol: 'celo-euro' },
]
for (const coin of coins) {
let coinDisplay = document.createElement('li')
let coinSymbolElement = document.createElement('p')
let coinIDElement = document.createElement('p')
coinSymbolElement.innerHTML = coin.symbol
coinIDElement.innerHTML = coin.id
coinDisplay.appendChild(coinIDElement)
coinDisplay.appendChild(coinSymbolElement)
coinsContainer.appendChild(coinDisplay)
coinDisplay.addEventListener('click', () => {
getCoinID(coin.symbol)
})
}
}
async function getCoinID(coinID) {
let currencyData = `https://api.coingecko.com/api/v3/coins/${coinID}`
let responseData = await fetch(currencyData)
let dataOfCoins = await responseData.json()
console.log(dataOfCoins)
}
addOnClick()
</script>
var baseUrl = "https://pokeapi.co/api/v2/pokemon/";
var pokemonid = document.getElementById('pokemon_id').value;
function fetchPokemon(){
fetch(`${baseUrl}&{pokemonid}`)
.then(response => {
return response.json()
})
.then(data => {
console.log(data);
})
}
fetchPokemon();
This code return me https://pokeapi.co/api/v2/pokemon/?offset=20&limit=20' at url, how can I change pokeomonid.value for return the number or name on the input?
i solve my problem using async await:
const insertPokemon = async(a) => {
const respuesta = await fetch('https://pokeapi.co/api/v2/pokemon/'+a)
const data = await respuesta.json()
const {value} = data
console.log(data)
nombre.textContent = data.name
id.textContent = "ID_"+data.id
img.src = data.sprites.front_default
pokeTypes.textContent = data.types[0].type.name;
pokeTypes2.textContent = data.types[1].type.name;
stats(value)
}
form.addEventListener('submit', (event) =>{
event.preventDefault();
pokeTypes.textContent = "";
pokeTypes2.textContent = "";
insertPokemon(pokeselect.value.toLowerCase());
})
as you see the code, on the handleUpdateFilter function the second "if" some how defaultCourseData is filtered as filteredData of the first "if". Thank you for helping me!
setup() {
const course = ref();
const defaultCourseData = null
const gettingCourse = async () => {
const { data } = await getCourse();
defaultCourseData = data
course.value = data;
};
const handleUpdateFilter = (data) => {
// data is filtering value
if (data.value.view) {
const filteredData = defaultCourseData.sort((a, b) => b.luotXem - a.luotXem);
course.value = filteredData;
}
if (!data.value.view) {
course.value = defaultCourseData // This case some how defaultCourseData filtered too
}
};
onMounted(() => {
gettingCourse();
});
return {
course,
handleUpdateFilter,
defaultCourseData
};
},
Your defaultCourseData variable isn't reactive.
Therefore it should be evaluated as null at every call.
Try this
defineComponent({
setup() {
const course = ref([]);
const defaultCourseData = ref([]);
const gettingCourse = async () => {
const { data } = await getCourse();
defaultCourseData.value = data
course.value = data;
};
const handleUpdateFilter = (data) => {
// data is filtering value
if (data.value.view) {
course.value = defaultCourseData.value.sort((a, b) => b.luotXem - a.luotXem);
}
if (!data.value.view) {
course.value = defaultCourseData.value // This case some how defaultCourseData filtered too
}
};
onMounted(async () => {
await gettingCourse();
});
return {
course,
handleUpdateFilter,
defaultCourseData
};
})
Edit: The actual issue here was, that the defaultCourseData always returned a sorted array as Array.prototype.sort() mutates the Array.
So making a copy solves the issue.
if (data.value.view) { course.value = [...defaultCourseData.value].sort((a, b) => b.luotXem - a.luotXem); }
I have a cloud function that "Joins" data from a list of documents in a collection.
I then return the result as an array, but I want to return the documentId as well (doc.id) in the list that i return.
How can i do that?
const restData = [];
//const userId = ctx.auth.uid;
const userId = 'dHAP1CNN6LhJWddQoTqyIkqIjhB2'; // !!! TEST ONLY
const all = await db.collection(`/customers/${userId}/lunch_cards`).listDocuments().then((snapshot) => {
snapshot.forEach(doc => {
const nextData = db.collection(`/restaurants`).doc(doc.id).get();
const newData = {...nextData, documentId: doc.id}; <-- This does not work only documentId isout in newData
console.log(util.inspect(newData));
restData.push(nextData);
console.log(doc.id);
});
});
const snaps = await Promise.all(restData);
const responseArray = snaps.map((s) => {return s.data()});
return responseArray;
I solved it!
Solution:
Just adding a new string to the array :)
const responseArray = snaps.map((s) => {
const snapData = s.data();
if (snapData) {
snapData['id'] = s.id;
}
return snapData;
});
I want the result of activeCustomers array inside the last then but I keep getting an error saying arrow function expects a return. Not sure how I can get activeCustomers?
const CreateCustomer = (storeData) => {
let activeOrganization = null;
storeData.Org
.getOrganization()
.then(function createCustomer(organization) {
activeOrganization = organization[0];
const dataArray= storeData.attributes;
activeOrganization
.createAttributes(
attributeType[0],
getSomeData(dimensions)
)
.then(function Properties(createdAttribute) {
updateCustomerProperty(createdAttribute, attributeType[0]);
});
activeOrganization
.createAttributes(
attributeType[1],
getSomeData(dimensions)
)
.then(function Properties(createdAttribute) {
updateCustomerProperty(createdAttribute, attributeType[1]);
});
}).then(() => {
activeOrganization
.getCustomers()
.then((cusomters) => {
const activeCustomers = [];
cusomters.map((customer) => {
activeCustomers.push(customer);
});
return activeCustomers;
})
.then((activeCustomers) => {
console.log(activeCustomers);
});
});
};
//Now I want the result of activeCustomers array inside the last then but I keep getting an error saying arrow function expects a return. Not sure how I can get activeCustomers?
I want the result of activeCustomers array inside the last then but I keep getting an error saying arrow function expects a return. Not sure how I can get activeCustomers?
In your example i think you received a warning. But still how to access to activeCustomers it depends on how you want to use it there are you storing it.
If you want to store it globally then you can store it like that
let activeCustomers;
....
.then((cusomters) => {
activeCustomers = [];
cusomters.map((customer) => {
activeCustomers.push(customer);
});
return activeCustomers;
})
But i think it's better to rewrite to async/await.
const CreateCustomer = async (storeData) => {
let activeOrganization = null;
const [activeOrganization] = await storeData.Org
.getOrganization();
const activeOrgPrs = attributeType.map(x => activeOrganization
.createAttributes(
x,
getSomeData(dimensions)
)));
const attrs = await Promise.all(activeOrgPrs);
attrs.forEach((attr, i) => {
updateCustomerProperty(attr, attributeType[i]);
})
const cusomters = await activeOrganization
.getCustomers();
return cusomters;
};
And you can use it like const customers = await CreateCustomer(someData);
or like CreateCustomer(someData).then((cusomters) => { activeCustomers = cusomters; return null;(if it keeps to return errors)});