I am working on a NativeScript app with Firebase. I have to display featured elements based on their status in the firebase array object. The status is
featured= true.
I want to filter directly in my function's url using query params (That's without using Headers in the http request).
Here is a sample of the firebase data structure
dishes
0
category:
"mains"
comments
description:
"A unique combination of Indian Uthappam (pancak..."
featured: "true"
id: 0
image: "https://www.inspiredtaste.net/wp-content/upload..."
label: "Hot"
name: "Uthappizza"
price: "4.99"
and here is my typscript angular based method:
getFeaturedDish(): Observable<Dish> {
return this.http.get<Dish[]>(baseURL + 'dishes.json' /**query placeholder */).pipe(map(dishes => dishes[0]))
.pipe(catchError(this.processHTTPMsgService.handleError));
}
To filter your structure on featured being "true", you'd use:
dishes.json?orderBy="featured"&equalTo="true"
Don't forget the double quotes around "true" as you are storing a string value, and not a boolean.
Also see the Firebase documentation on filtering data with the REST API.
Related
I have an update endpoint that when an incoming (request) contains a site name that matches any site name in my job site's table, I change all those particular DB entries status to "Pending Transfer" and essentially clear their site location data.
I've been able to make this work with the following:
async function bulkUpdate(req, res){
const site = req.body.data;
const data = await knex('assets')
.whereRaw(`location ->> 'site' = '${site.physical_site_name}'`)
.update({
status: "Pending Transfer",
location: {
site: site.physical_site_name,
site_loc: { first_octet: site.first_octet, mdc: '', shelf: '', unit: ''} //remove IP address
},
//history: ''
}) //todo: update history as well
.returning('*')
.then((results) => results[0]);
res.status(200).json({ data });
}
I also want to update history (any action we ever take on an object like a job site is stored in a JSON object, basically used as an array.
As you can see, history is commented out, but as this function essentially "sweeps" over all job sites that match the criteria and makes the change, I would also like to "push" an entry onto the existing history column here as well. I've done this in other situations where I destructure the existing history data, and add the new entry, etc. But as we are "sweeping" over the data, I'm wondering if there is a way to just push this data onto that array without having to pull each individual's history data via destructuring?
The shape of an entry in history column is like so:
[{"action_date":"\"2022-09-06T22:41:10.232Z\"","action_taken":"Bulk Upload","action_by":"Davi","action_by_id":120,"action_comment":"Initial Upload","action_key":"PRtW2o3OoosRK9oiUUMnByM4V"}]
So ideally I would like to "push" a new object onto this array without having (or overwriting) the previous data.
I'm newer at this, so thank you for all the help.
I had to convert the column from json to jsonb type, but this did the trick (with the concat operator)...
history: knex.raw(`history || ?::jsonb`, JSON.stringify({ newObj: newObjData }))
In my app, i want to manage access to some data, according to the user's group. I'm using Firestore.
Here is how i have my data set in my database :
groups:
0PkuNM6RmQi0R4kNrN6E:
name: "Group 1"
users:
user1_uid: true
user2_uid: true
user3_uid: true
UaN0fsM8aK1lGGPKAhPp:
name: "Group 2"
users:
user4_uid: true
documents:
tGKE1rmax4fwc8xm9V5a:
title: "Document 1"
desc: "Description 1"
group: "UaN0fsM8aK1lGGPKAhPp"
As an entry point, i have only the user_uid of my current user. I want to fetch all documents that my group can access to.
Is there a way to manage this only with "rules" from database, and by making a basic request like the following :
databaseDocumentRef..collection("documents").get()
I had some attemps with the rules but i didn't succeed in.
Thanks.
You can't use rules to filter documents by some criteria. Please read the documentation: Rules are not filters. You will have to query the database for exactly the data you require, perhaps by multiple queries. Rules can only help you enforce the requirements of individual queries, not change their results.
I am using Angular 4 with Typescript and ASP Core with C#. I am trying to combine objects together before calling a post to my web api. Currently I have two objects with values that I want to post as one object.
Let say I have two interfaces
export interface Student {
name: string;
}
export interface Teacher {
name: string;
}
And I wanted my json in my body of the post to look like this as my ASP Core is expecting this as a parameter to the web api request.
{
"StudentName": "John Doe",
"TeacherName": "Jane Doe"
}
I am using the following to create the json for the body but this puts it into two separate records in the json. This also does not allow me to select specific properties if my interfaces had more than what the request required.
const body = JSON.stringify({ student, teacher });
Next I tried to access only the specific member variables in the JSON.stringify method, but for some reason it does not work. In addition, if this even worked it would end up with two properties with the same name.
const body = JSON.stringify({ student.name, teacher.name });
I am not sure if this is a typescript, javascript or angular issue. I tried searching online for some help but I don't think I am using the right keywords.
Thanks in advance!
The problem is that you're not labeling your object items:
const body = JSON.stringify({ StudentName: student.name, TeacherName: teacher.name })
is the expected format.
I have an angular application that requests data from a JSON API. A sample API response might be:
{
id: 1,
name: 'JJ',
houseId: 2
}
In my angular application I will have models representing a User, which also has a reference to a House object:
{
id: 1,
firstName: 'JJ',
surname: '',
house: {
id: 2,
address: 'XXX'
}
}
The application model and API responses differ in that there is one field for the name in the API response, but two in my application model. Is there an 'angular' way I can do some transformation from an API call response object to my application model to ensure that I am always dealing with consistent objects in my controllers/services?
Related to this, the API responds with the database id of the house object associated with that user, and not with the full house object included in the JSON. Is there a way to set my object up to automatically resolve this when needed?
As an example, I would like to display this user, with his address. If the object was fully resolved I could use 'user.house.address'. However, using the plain JSON response object, this would be undefined. Instead of having to explicitly resolve the house object by using the house API with the houseId, I would like this to happen 'behind the scenes' by previously stating how such an id would be resolved if the object is accessed.
Or am I approaching this the wrong way and the API response should be used to dictate the data structure of my application and explicit lookups via object id's is the preferred way?
I am trying to use the find method to return documents that meet multiple criteria like so:
db.issues.find({ 'title.name': name, 'title.year': year, 'title.publisher': publisher }, function(err, issues) { ...
I get an empty array back as a result.
If I try the same query directly in the mongodb console, I get the results back as expected.
In mongojs, I am able to retrieve results if I only supply one attribute, like so:
db.issues.find({ 'title.name': name }, function(err, issues) { ...
So, how do I perform a search using multiple attributes? Do I need to use a different method?
UPDATED 2/2/2014 7:56
The issue schema is as follows:
{
title: { name: 'Some title name', year: 1999, publisher: 'Some publisher' },
number: "122",
variant: null,
print: "1",
year: "1999",
certification: { type: "Some certification", grade: 10 },
url: "http://www.someurl.com",
sales: []
}
There is no difference in how mongojs handles queries as it's intention is to mimic ( somewhat ) the behavior of the mongo shell.
Are you sure your criteria actually returns something? As it stands you possibly have an issue there. If you still cannot diagnose this, post up the result you expect to get and some of your schema as an edit. Don't forget to check your var's that numbers aren't strings and vice versa.