Can't use variable as field name updateDoc (firebase) - javascript

How can i write a variable there? This is React.
${} and ""+"" are not working
Pls i neeed help
const id = 'currentChat[0].
id';
const fn = async () => {
await updateDoc(contactsRef, {
`${id}.chatHistory`: arrayUnion(message),
});};
fn();

If you have an expression, you need to put it inside []. These are called computed property names.
You need to use
[`${id}.chatHistory/`]
Read this.

Related

Function's parameter used inside a previously defined variable (String) returns undefined

trying to declare a variable for an API call (There are different website versions for API calls so want to have it dynamically interchangeable while having clean code).
It goes like this:
const getAbi = async (addy) => {
const url = `https://api.arbiscan.io/api?module=contract&action=getabi&address=${addy}&apikey=${arbiscanKey}`;
const abi = await axios.get(url);
console.log(abi);
return abi;
};
Now, its supposed to take the function parameter (Named "addy") and input it inside the API call link. For some reason, it does not do that. It gives me error:
const arbiscanGetAbi = `https://api.arbiscan.io/api?module=contract&action=getabi&address=${addy}&apikey=${arbiscanKey}`;
^
ReferenceError: addy is not defined
Not sure how to get it to realise i am referring to "addy" from the function parameter, thanks!

Firestore References

i want to add a reference field in a document into Firestore using nodejs but i can't do it.
I wrote this code:
async function pushType(IDType) {
const size = await getID();
const IDClient = (size).toString();
const docRef = firebase.firestore().collection('Clients').doc(IDClient);
await docRef.set({
ID_Type: firebase.firestore().doc('Types/'+IDType).ref,
});
}
async function getID(){
const snapshot = await firebase.firestore().collection('Clients').get();
return snapshot.size;
}
The error is: "Function Document.Reference.set() called with invalid data. Unsupported field value: undefined (found in field ID_Type in document Clients/7)" where 7 is the ID of the document where i want to add the field ID_Type.
Can anyone help me to understand what i'm wrong and how can i fix it? Thank you
Your code firebase.firestore().doc('Types/'+IDType) returns a DocumentReference. As you can see from the API docs, there is no property called ref on that, so it will be undefined. In fact, that DocumentReference is exactly what you want to provide to Firestore here. So just remove ref:
await docRef.set({
ID_Type: firebase.firestore().doc('Types/'+IDType)
});
When you provide a DocumentReference object to a Firestore document like this, it will create a reference type field.

CSVtoJSON returns UnhandledPromiseRejectionWarning: ReferenceError: JavaScript

I am trying to convert CSV from a CSV file to JSON.
I have the following code
(async() => {
csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
let latest_provinces_confirmed = source;
});
console.log(latest_provinces_confirmed)
...
and when I run it, I get
UnhandledPromiseRejectionWarning: ReferenceError: latest_provinces_confirmed is not defined
How can I get the result of the CSVtoJSON into a variable to use later
Thanks in advance
The variable 'lastest_provinces_confirmed' is declared inside the anonymous function so you cannot access it outside that function. Thats why console.logging variable doesn't work.
You can try to pass that variable outside of that function by returning it OR you can forward declare that variable outside those functions to be able to access it.
Something like this might work:
let latest_provinces_confirmed = csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
return source;
});
Remember to take account that you are working with async functions so you have to make sure that function csvToJson() has run before using 'latest_provinces_confirmed'. You can check use of "await" in order to do that!
You are doing a simple and a beginner level mistake. The scope of "let" is only inside the block. Change your code as below and it should work.
(async() => {
let latest_provinces_confirmed;
csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
latest_provinces_confirmed = source;
console.log(latest_provinces_confirmed)
....
});

Puppeteer identifier string variable won't parse; Unsure why

I'm trying to have a string be used inside a puppeteer string, it won't work for some reason.
Specifically with this code
await page.waitForSelector('div[class = "sh-dlr__list-result"')
When i try to parse in a variable
let identified1 = 'div[class = "sh-dlr__list-result"'
so making
await page.waitForSelector(identified1)
It won't work. This is really limiting, is there a way around this issue?
This is the expanded code
https://jsfiddle.net/hewlbern/6p7kdozt/10/
Run it in your computer, jsfiddle unsure if I can run it from there.
I believe it is creating a cors error now - very weird! Why would using a variable create a cors error : /
Thanks!
The reason is because you're declaring identified inside the page.evaluate(). So, when you do the following it's already out of scope.
if (currentPage < pagesToScrape) {
console.log(identified1);
await Promise.all([
await page.click(buttonSelector),
await page.waitForSelector(identified),
]);
}
You did log the identified1 but you're using identified for the selector.
You'll have to pass the identifier2 to the pageFunction like so:
let newProducts = await page.evaluate(({ identifier2 }) => {
// ...
},{ identifier2 });
See here some examples:

How to reference local variables from within a mongoose `$where()` query?

How do I reference a local variable from within a $where query in Mongoose?
For example:
var testName = 'Some Dude';
Cab.$where('this.name === testName').exec((_err, cabs) => {
// Do something...
});
If testName will be decided on runtime, how should it be referenced in the query?
My actual query is a bit complicated so I need to execute a Javascript expression.
I've tried using this.testName which didn't work either.
One way is to use ES6 template literals, e.g.:
var testName = 'Some Dude';
Cab.$where(`this.name === ${testName}`).exec((_err, cabs) => {
// Do something...
});

Categories

Resources