How to add an array to Firebase Firestore Swift - arrays

I am trying to update the data in a document in Cloud Firestore with an array however whenever I try to do this I get an error: Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: __SwiftValue (found in field upvotes)'. I would really appreciate it if someone could help me understand what I am doing wrong and how I can fix this. Thanks. Code is below.
Code for updating array-
db.collection("forum").document(data[0].id!).setData(["upvotes": data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")!)])

You must pass an array to Firestore to update an array property. What you're doing is passing an element of an array. The remove(at:) method, when used like this:
let removedElement = someArray.remove(at: someIndex)
will remove the element but return the value of that removed element; it doesn't return the updated array. You must first remove the element and then get the truncated array:
someArray.remove(at: someIndex)
let updated = someArray
Therefore, first remove, then update:
data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")! // first remove
db.collection("forum").document(data[0].id!).setData([
"upvotes": data[0].upvotes!) // then pass
])
However, I'd recommend reformatting the code because it doesn't read very well, IMO, and there is too much force unwrapping for my taste.

Related

How to display element from an array

In the below image there is an array given to me on doing console.log
Array on doing console.log
The name of the array is commentData
if i do commentData.name i get the name
but when i do commentData.replyoncomment.Rname or commentData.replyoncomment[0].Rname
i get undefined even on commentData.replyoncomment
how do i solve this problem
if you need anything else ask me.
Update:
Thats how i turned commentData to an array
const convert = JSON.stringify(comment)
const comments = Object.values(JSON.parse(convert))
const commentData = comments
when i do commentData.name or commentData.commment
it give me the name/comment
but when i do commentData.replyoncomment i get undefined.
see the first image to understand better
on running this i get
{JSON.stringify(commentData)}
[[{"_createdAt":"2022-08-07T14:34:40Z","_id":"dqDIn547oWb3s2Y4OEwhMK","comment":"sdfanj akf js","name":"nkfndm nj","replyoncomment":[]},{"_createdAt":"2022-08-07T03:13:01Z","_id":"SD6gTXwnOmN4hstl5nkSbH","comment":"hello, I'm the admin of the webstie","name":"Mian Mohid Naeem","replyoncomment":[{"Rcomment":"gfdhhh","Rname":"gff","_createdAt":"2022-08-07T10:17:33Z"},{"Rcomment":"print("Hello world")","Rname":"programmer","_createdAt":"2022-08-07T03:13:40Z"}]}]]
anything else you need to see in the code ask me...
You should map the commentData.replyoncomment to, cause it is an array too, and React can't render arrays.
So nest an additional map to the commentData.replyoncomment.
I was willing to help you, if you have any more questions, ask me.

How to loop through and edit a JsonArray of objects in Scala

I have a variable descriptions that is an Option[JsonArray] (JsonArray results from calling getJsonArray on a JsonObject). descriptions is a list of objects that I'm getting back as a response from a server. Each object has a structure like so:
{age: String,
number: Long,
link: String}
I'd like to loop through this JsonArray and edit the link field to a certain value that is returned from a function. I don't even need to edit it in place and can take a new array of objects. In JS, it might be as simple as descriptions.map(d => ({...d, link: someValue})) but I'm not sure how I need to transform the JsonArray to get this working in Scala. Any help is appreciated!
#mysl didn't provide necessary details as mentioned in the comments. So I'm going to speculate that the approach you've proposed didn't work and you want to understand why.
Most probably you've assumed that JsonArray would be mutated when you .map a lambda over it (which is the case for Javascript I guess). That's not true in Scala case. I.e. when you map a list over you've got another list. I.e. .map, as the name assumes, is just a way to map one collection to another.
So what you need to do is:
val originalJsonArray = ...
val updatedJsonArray = originalJsonArray.map { description =>
description.copy(link = description.link.replace("foo","bar"))
}
println(s"originalJsonArray=$originalJsonArray, updatedJsonArray=$updatedJsonArray")
I hope that helps, though I'm not sure I guessed your problem correctly.

How to retrieve specific value of an array from firestore

I am retrieving specific data as an array from firestore, the value is the there on the console.log(), but i cant seems to retrieve the specific data from the array itself,
here's my event.ts
import { Event } from '../../models/event';
invitedEvents: Event[] = [];
this.invitedEvents = invitedEvents;
console.log(invitedEvents, invitedEvents.name);
on console.log()
as you can see the invitedEvents.name return value of undefined, i'm sure you guys know the proper way of retrieving the value of name, send help.
The object is in the array, so you have to access object from array first invitedEvents[0]
console.log(invitedEvents, invitedEvents[0].name);
Here you have to invitedEvents is an array. So if you need to access you can't directly like invitedEvents.name like this, here is the few ways to access.
for(let item of invitedEvents){
console.log("Data",item);
console.log("Specific Name",item.name);
}
This example is used to print multiple values.
If you have only one value.Simply do like this.
console.log("Specific Name Second way", invitedEvents[0].name);
I hope its helps you.
Thanks,
Muthu

Pass current object first Array as3

Is it possible to pass an array whilst placing the currentTarget object first in the array?
I've searched online and only found reference to index, indexOf etc but no resources on how to do this anywhere.
I can send the array no problem but it's always as first loaded, through selecting a different object it must be possible to ammend the array or splice the new object to the beginning but would really be grateful of any assistance as to how to achieve this.
Not 100% sure what you're asking, but you can use the Array::unShift(...args) method to insert an object at the beginning of an array.
Edit:
From your comment below, I think I get what you're saying. You can just swap the item you're looking for with the one at the beginning:
var index = array.indexOf(event.currentTarget);
array[index] = array[0];
array[0] = event.currentTarget;
or something similar to that anyways.

Full Object, but empty array. $.each not hitting

![information is clearly there, but not being found][1]
Does anyone have any idea what I can do to my code to make this $.each / _.each work? (I'm using both jquery and underscore, either will do)
The Array[0] concerns me. Maybe that is why the .each is getting stepped over.
The two console.log's on either side of the each log the entire object perfectly, but when I break at the each and type "regionedAps" into the console, I get an empty array.
You have an array that you are treating like an object. You should make regionedAps an object, then .each will iterate over it properly.
var regionedAps = {};
I don't think you need $.makeArray()

Resources