Firebase dynamically change orderBy type - javascript

I am using Firebase to create a search function as follows
Firebase.database.ref('database-location').once('value').then(function(snapshot){
//do some filtering on results
});
However i want to dynamically sort the results using either:
.orderByKey()
or
.orderByChild('price')
I tried using bracket notation to select the above (the vars are decided from an if statment):
var orderByType = 'orderByChild'||'orderByKey'
var orderByVal = 'price'||undefined
Firebase.database.ref('database-location')[orderByType](orderByVal).once('value').then(function(snapshot){
//do some filtering on results
});
This works well for sort by value, but when using sort by key, firebase returns an error : "Query.orderByKey failed: Was called with 1 argument. Expects none."
Is there a way to dynamically remove the argument completely?
or any other way to switch between the two order by types?

Related

Filter method gives back a proxy array

I want to filter an array of objects using Vue Composition API. I'm using filter method, but it changes the output, so my app crashes.
My code looks like this:
rows = store.history.rows;
let items = rows.filter(item => item.department == item.department); //I know this will be always true
console.log(rows);
console.log(items);
The output will be totally different, that's why I cant use it...
How could I properly filter the rows without changing the output?

How to prevent the sequelize from creating the output clause

When I do a create using sequelize it returns me the response i.e. the newly created entry row in the response,
Sequelize Create Object Code:
let createdObj= await sequelize.ModelName.create(modelObject,{ transaction :t, //more options can be added here, need some value of option that prevents the output inserted })
Below is the query created:
INSERT INTO [TABLE_NAME] ([COL1],[COL2],[COL3],[COL4]) OUTPUT INSERTED.* VALUES (#0,#1,#2,#3,#4)
Now I don't want the output clause to be part of the query, I want a simple insert like:
INSERT INTO [TABLE_NAME] ([COL1],[COL2],[COL3],[COL4]) VALUES (#0,#1,#2,#3,#4)
I don't want the output clause to be part of the query.
How can I achieve this in at the query level as well as at the model level? In some Create operations, I want the output clause and in some create operations, I don't want.
EDIT 1
On Further research I found an option called { returning: false } this does what is required i.e. create an insert query like this INSERT INTO [TABLE_NAME] ([COL1],[COL2],[COL3],[COL4]) VALUES (#0,#1,#2,#3,#4) but now the Sequelize is breaking because it's expecting those values back in return idk why?
C:\Users\MG265X1\project\node_modules\sequelize\lib\dialects\mssql\query.js:389
id = id || results && results[0][this.getInsertIdField()];
^
TypeError: Cannot read property 'id' of undefined
at Query.handleInsertQuery (C:\Users\MG265X1\project\node_modules\sequelize\lib\dialects\mssql\query.js:389:39)
Turns out if an autoIncrementAttribute is present in the model, it will look for the output clause, removing the attribute {autoIncrement: true } from the model hasn't helped as IDENTITY_INSERT cannot be null. How do I move ahead on this??
Edit 2 I could get it working with a combination of { returning: false } and {hasTriggers: true}. Have hasTriggers Attribute as true in your Model, this will allow you to single creates but for bulk Creates pass option returning: false at the time of bulkCreate.
Note: When using bulkCreate with { returning: false } you'll not be able to get the autogenerated Id, It's a trade-off that we had to live with as we want
bulkCreate to work with triggers, we ended up fetching the Id later from DB
Seems I raised this issue but was closed as it wasn't good SSCCE

passing different arrays parameters in pact

I have two api calls , with the only difference being in the array parameters :
// it is used for displaying whole list
Query 1 : limit=25&var1=[deleted=false]
//it is used for displaying on basis of id
Query 2 : limit=25&var1=[deleted=false,id=10005]
I tried using term , also something like but it takes both the query as one
var1: term({
generate: '[deleted=false]',
matcher: '[deleted=false]'}),
var1: term({
generate:'[deleted=false,id=10005]',
matcher:'[deleted=false,id=10005]'}),
Also , i passed the exact query but it doesn't work
Any suggestions on how to do it ?
If you want to using dynamic legnth array type by parameter in javascript, Use this.
var [deleted, id, ...any] = parameter;
console.log(deleted, id);
In case, if you throw parameter([deleted]), log be printed by "deleted, id is undefined".

Get field value from mongoDB and assign to a var gives undefined

I am using MeteorJS and trying to get the value of a field from MongoDB and assign to a variable. But when want to print to console, it gives all the time 'undefined'. It works fine in HTML template, but i need to store the value in a var in .js file.
var num = ButtonsList.find({_id:'ZcLkjSwNGTpgHkoeq'});
var n = num.button1;
console.log("button number is: "+n);
The code below works fine by the way, if i want them to output in the browser. It outputs the buttons numbers in html using {{}} namespace. But as I said, i need to store the values in variables.
ButtonsList = new Meteor.Collection('list');
Template.theList.helpers({
'buttons': function(){
//return ButtonsList.find().fetch();
return ButtonsList.find('ZcLkjSwNGTpgHkoeq');
}
});
ButtonsList.find() returns a cursor.
ButtonsList.find().fetch() returns an array of buttons.
ButtonsList.findOne() returns will return a single button.
ButtonsList.findOne().fieldName will return the field fieldName of the button that was found.
The reason it works with the {{#each}} template block helper is that each blocks know how to iterate over cursors.
Your using Find , doesnt that mean your getting multiple reccords back? Shouldnt you be using FindOne instead? otherwise youll get an array of objects which means youd have to use num[i].button1 to get to the value.

Filtering contacts to display or show only set number of contacts

My app is calling pickMultipleContactAsync method that returns a list of contacts. If I want to filter only particular contats to display or show only a set number of contacts, does WinJS provide any specific methods or patterns to do this? I know since its a list, I probably can use some standard predicate type of thing to do this type of filtering, but I wonder if there is anything specific WinJS API to do this.
The WinJS.Binding.List uses a feature called "projection" to create a second list filtered by a predicate which will automatically update itself as the origin list changes.
Use the createFiltered function, then you can use the resulting list for example in a WinJS.UI.ListView.
Array.filter can be used in this case.
var picker = Windows.ApplicationModel.Contacts.ContactPicker();
picker.commitButtonText = "Select";
picker.pickMultipleContactsAsync().then(function (contacts)
{
var contactsStartingWithPrefixPa = contacts.filter(function filterContacts(contact)
{
if (contact.name.match(/^Pa/))
return true;
return false;
});
});

Categories