geoFirestore - add new document with array as one of the document data property - database

While i try to add new document to the DB with geoFirestore and try to set one of the property as array with DocumentReference (refers to a document location in a Firestore database) i get the following error:
FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: a custom object
If i set there an empty array its success.
BTW if i set there a string or number its work too
The inseration code:
await geocollection.add({
coordinates: new firebase.firestore.GeoPoint(latlng.lat, latlng.lng),
arrayOfRef: [newRef]
});
I try to use Object.asset() to the newRef but get the same result.
Thanks for the helpers.

So in a lot of cases geofirestore minimally modifies the doc you give and then passes it through to Firestore. In this case it looks like Firebase/Firestore is throwing the error, not geofirestore.
I suspect that the issue is with the reference you're using. If that reference is provided by geofirestore, it's not necessarily a DocumentReference that Firestore uses, it is a GeoDocumentReference. You'll likely want to get the actual DocumentReference from the native property:
await geocollection.add({
coordinates: new firebase.firestore.GeoPoint(latlng.lat, latlng.lng),
arrayOfRef: [newRef.native]
});
Let me know if that helps! I'm also making a few assumptions as to your code, so seeing more of what happens before you call the add method would be awesome.

Related

firestore react "TypeError: is not iterable" when append to array

The data is the database is:
Comment:
{
uid1:"a"
uid2:"b"
}
and I wanted to add CommentContent which is:
uid3:"c"
I am trying to read from the database, append data to the data then update the database, and below is my code:
const OriginalContentSnap = await getDoc(doc(db, "Comment", curSnap.id));
const OriginalContent = OriginalContentSnap.data();
const CommentContent = {[uid] : commentDocID.id};
CommentContent = [...OriginalContent, CommentContent];
however, everything is fine until the last line where I am getting the following error:
Uncaught (in promise) TypeError: OriginalContent is not iterable
any help will be greatly appreciated.
The“OriginalContentSnapdata ()”,returns an object, which you can't iterate over ,please check this helpful DocumentSnapshot document regarding the details of the Snapshot data variables and data types,on the returned object.
You should make sure your resulting object has items and it must be an array. Then, change your iterator, so it generates an array based on your fetch results.
Check the below for similar examples which will be helpful:
Uncaught Typeerror Object is not iterable
Type error undefined is not iterable
Object is not iterable cannot read property symbol
Object null is not iterable cannot read property symbol

Apollo Client readFragment with custom id (keyFields)

For ref, using "#apollo/client": "^3.5.5",
I've defined my typePolicies like so as suggested in docs:
HistoricalData: {
keyFields: ["variable", "workspace"],
fields:{...}
}
and when my cache is built, I am expecting my cacheId to be like
<__typename>:<id>:<id>
HistoricalData:${props.variable}:${props.workspace}`;
but instead, when I look in the Apollo cache, it's been created using the keyField names and the values in an object, such as
HistoricalData:{"variable":"GAS.TOTAL","workspace":"ABC"}
instead of
HistoricalData:GAS.TOTAL:ABC
so when I try to readFragment it returns null
client.readFragment({
id: `HistoricalData:${props.variable}:${props.workspace}`,
fragment: apolloGQL`fragment MyHistorical on Historical {
variable
workspace
}`})
It does actually return a value from the cache if I create my id in the structure that exists in the cache and readFragment using this.
Has anyone else noticed that Apollo client is not creating the cache id's in the structure that they describe in the docs?
After some research I came upon the correct way to handle this case. I know that you have already moved on, but just in case anyone else has the same problem in the future, here goes:
As described in the documentation for customizing the cache ID, the cache ID will be an stringified object, as you pointed out. It's not quite explicit in the documentation, but at this point in time it provides this nested example for a cache ID:
Book:{"title":"Fahrenheit 451","author":{"name":"Ray Bradbury"}}
But as users we don't have to preoccupy us with the format of this ID, because there's a helper for that, called cache.identify.
For your specific case, you could use something like this:
const identifiedId = cache.identify({
__typename: 'HistoricalData',
variable: 'GAS.TOTAL',
workspace: 'ABC'
});
cache.readFragment({
id: identifiedId,
fragment: apolloGQL`fragment MyHistorical on Historical {
variable
workspace
}`
});

TaxonomyFields in SPFx

Currently I'm creating a new form in SPFx. When reading the fields of the list there is an taxonomyField.
I'm using the PnP Taxonomy control to display the taxonomy labels.
I would like to get the termset id from the taxonomy field and using the following code
var taxField = field as SP.Taxonomy.TaxonomyField;
ctx.load(taxField);
await new Promise((resolveTax, rejectTax) => {
ctx.executeQueryAsync(()=> {
let termSetID = taxField.get_termSetId();
console.log(termSetID);
resolveTax();
}, (sender,args) =>{
console.log("Could not retrieve taxonomyfield termsset id: " + args.get_message());
rejectTax();
});
});
}
I Always receive the following error:
TypeError: Cannot read property 'TaxonomyField' of undefined
at eval (eval at Type.parse (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:10143), :1:13)
at Function.Type.parse (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:10143)
at SP.ClientRequest.$3K_0 (https://...sharepoint.com/_layouts/15/SP.Runtime.js:2:51794)
at Array. (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:307)
at https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:51370
at Sys.Net.WebRequest.completed (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:89652)
at XMLHttpRequest._onReadyStateChange (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:84251)
Anybody a suggestion to fix this?
I suggest to use open source tools to work with SharePoint's Taxonomy, for example react-taxonomypicker
This is an elegant Taxonomy Picker control built with TypeScript for React. Initially built for use in Office 365 / SharePoint.
Features:
Retrieve Terms from a Term Set by Term Set GUID.
Support for large Term Set using Async mode
Use SP.Taxonomy.js
Use Promise (polyfill it if needed IE)
You can test it here: https://jquintozamora.github.io/react-taxonomypicker/

ng2-smart-table display list of object

Is there a way to display a list of objects in a single table cell for ng2-smart-table? I have tried creating a renderComponent but I am getting an empty value. Another question is will the filtering and sorting still work for this?
As I understood , You have a object and you want to display that data in ng2-smart-table.
For that follow this step.
import { LocalDataSource } from 'ng2-smart-table';
source : any = LocalDataSource;
When you call API then you have to set that data in source.
this.apiService.POST({}, 'getProductList').subscribe((res) => {
console.log(res);
this.source = new LocalDataSource(res.data); // Set response as per your res.
});
As you can see I have also set one array and that array has objects of data and I have set in table.
I hope this may help you. :)

Query arrays values with Firebase

Is there any way to on Firebae to filter data in an array?
I have this model on my Firebase:
-KABIGeWnBMUKjLTcvp8
deviceToken:"7DE60240CB4B712F05A009F32358610C1327917E7E68409..."
favorites
0:"Masha"
1:"moksha"
name:"juan"
And the problem is that I can't find any method to get all "users" that contain a certain value on the "favorites" array field.
Nope, that's not an option See firebase equivalent to sql where in ().
Instead: invert your data structure to make this query possible:
items_by_favorites
"Masha"
"-KABIGeWnBMUKjLTcvp8"
"moksha"
"-KABIGeWnBMUKjLTcvp8"
Now you can look up the item keys for Masha with a simple read: ref.child('items_by_favorites/Masha') and then load each item:
ref.child('items_by_favorites/Masha').on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key();
ref.child('items').child(key).once('value', function(itemSnapshot) {
console.log(itemSnapshot.val());
});
});
})
First of all your question is answered deep in the guide for retrieving data, which is where I got this answer. It's under complex queries, then range queries, should you want more info.
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
console.log(snapshot.key());
});
The basic idea is that you need to first order the reference by a common child value, and then call .equalTo() to end up with a query that yields what you want.
Also you can call order by child like
ref.orderByChild("height/sublevel")
To drill deeper in the tree.
FirebaseFirestore.instance.collection('your collection name').where('favorite', arrayContains: 'Masha').snapshot();

Resources