Unable to put some scope data in json in Angular js - javascript

When trying to build a json object from data in $Scope variables, some of the values are not reflected in json.
Below are the details:
1. In my Controller :
$scope.cart.books = [ ];
$scope.cart.date = "";
$scope.cart.pending = "";
var data = {
date: $scope.cart.date,
pending: $scope.cart.pending,
books: $scope.cart.books
};
I am not getting the value of 'date' and 'pending' when I update it in Firebase, but values of 'books' array is correctly updated in Firebase.
But when I log the values of 'date' and 'pending' in console, I am getting the them.
Can someone please suggest where I am going wrong?

Found the solution.
I was using a function in another service to get the values.
It was executing as soon as the code was run and assigning default values in var data.
So I made a new function and only when it is invoked, the values will be filled in var data.
Thank you everyone for your suggestions specially #Patrick Evans and #mak.

Related

'HttpResponse.Data' and the 'results' attribute values of executed breeze query are different

I'm currently using BreezeJs in an asp.net web application and identified that 'HttpResponse.Data' and the 'results' attribute values of executed breeze query are different in random cases.
The identified scenario: when the database is manually updated using a stored-procedure and set several data records values to empty.
return EntityQuery.from('SampleEndpointName')
.withParameters({ Id: sampleId})
.using(self.manager)
.execute()
.then(querySucceeded, this._queryFailed);
function querySucceeded(data) {
var sampleData;
if (data.results.length > 0) {
sampleData = data.results;
}
return sampleData;
In above-mentioned scenario, a network call triggers and fetch the correct values but not updates 'results' of returned breeze object. The 'results' retain previous values. Tried adding .using(MergeStrategy.OverwriteChanges); but didn't work. Any clues on fixing this issue?

How to get values and $id from $firebaseArray in angularJS

Hi I'm not a pro in firebase, just started working in it. After searching through many places I couldn't find solution to my problem.
My firebase database structure is as follow:
I want to retrieve data of a specific profile along with unique $id generated by firebase and.
$scope.login = function(users)
{
firebase.auth().signInWithEmailAndPassword(users.email, users.password)
.then(function(result)
{
var ref = firebase.database().ref("/profiles");
var examination = $firebaseArray(ref.orderByChild('uid').equalTo(result.uid));
console.log(examination);
and result I'm getting is
like this
Can anyone help me in this regard i.e. how to get values from result. Thanks In advance.
If you want to retrieve data and log it in your code, don't use AngularFire and stick to the JavaScript SDK:
$scope.login = function(users) {
firebase.auth().signInWithEmailAndPassword(users.email, users.password)
.then(function(result) {
var ref = firebase.database().ref("/profiles");
var examination = ref.orderByChild('uid').equalTo(result.uid);
examination.on('value', function(snapshot) {
console.log(snapshot.val());
});
You'll note that I added on('value', which is how you tell the Firebase SDK to start loading the data from the database. This and much more is covered in the Firebase documentation for web developers and I highly recommend that you read that end-to-end. A few hours spent there, will more questions than you imagine.
If you prefer sticking to AngularFire, then you should stop using console.log for checking loading status. From the AngularFire quickstart:
... $scope.data is going to be populated from the remote server. This is an asynchronous call, so it will take some time before the data becomes available in the controller. While it might be tempting to put a console.log on the next line to read the results, the data won't be downloaded yet, so the object will appear to be empty.
Instead, you can simply show the data directly in the HTML template:
The easiest way to log the data is to print it within the view using Angular's json filter. AngularFire tells the Angular compiler when it has finished loading the data, so there is no need to worry about when it be available.
<pre>{{ data | json }}</pre>
This last snippet comes from handling asynchronous operations in the AngularFire guide.
Use the $loaded() promise to see the value:
$scope.login = function(users)
{
firebase.auth().signInWithEmailAndPassword(users.email, users.password)
.then(function(result)
{
var ref = firebase.database().ref("/profiles");
var examination = $firebaseArray(ref.orderByChild('uid').equalTo(result.uid));
//console.log(examination);
examination.$loaded().then(function (exam) {
console.log(exam);
});
It is important to realize that invoking a $firebaseArray method immediately returns an empty array. Once the data is returned from the server the existing reference is populated with the actual data.
The $loaded() method returns a promise which is resolved when the array data has been downloaded from the database. The promise resolves to the $firebaseArray.

How can I add a special Mongo expression to a JSON object?

So I'm aware how I should add any regular string/object to a JSON object - however, I need to add the following Mongo command to my JSON object:
$set : { "author" : req.body.name }
As you can see, it's not as simple as just doing:
myJsonObject.author = "$set..." etc.
Sorry if this is a dupe question - this isn't the easiest subject to Google without tons of unrelated answers popping up.
The reason I need to know this because I want to be able to build a JSON object based on what the user has changed in the form. So a new problem I have encountered is that even if I can build the JSON object with multiple items to change, MongoDB only updates the last item in the object. My example JSON object is here:
updateItem = {
$set : { "name" : "Squid bonobo" },
$set : { "author" : "Mardy Bum" }
};
and the snippet of code which issues the update command is as follows:
updateData.update(searchItem, updateItem, false, true, function(err, results) {
console.log(results);
db.close();
});
I found this solution at the following git site.
Can anybody help with either of these questions?
Thanks for your help :)
Cameron
You need to form your JSON in the below structure:
updateItem = {
$set : { "name":"Squid bonobo","author":"Mardy Bum"}
}
Javascript Objects can be declared with duplicate keys/properties, but their values get over-written, with the latest bound value for the key, in any order.
Here, $set is a key/property which has been set twice to the same object referenced by the variable updateItem, hence only one value, that is last encountered, will be associated to the key. In this case, the value that takes precedence is:
$set:{ "author":"Mardy Bum" }
and the final query that gets executed becomes,
updateItem = {
$set : { "author" : "Mardy Bum" }
};

Backbone.js attribute / fetching seems to work funny

So, I have the following code that access my rest api:
Employees.Employee = Backbone.Model.extend({
urlRoot: 'api/employee'
})
var daryl = new Employees.Employee({id:17})
daryl.fetch()
console.log(daryl.attributes)
Now, when I console.log the attributes, the daryl object is set up like this roughly:
daryl = {
attributes:
[0]: {
id: 17,
first: 'Daryl',
last: 'xxxx',
email: 'xxx'
},
id: 17,
watchers...
protos...
}
So trying to daryl.get('first') results in undefined. Everything else is stored in the object in the array at index 0. Why is this? I'm a newbie here but this is definitely not how most of the tutorials seem to show how backbone works.
So if I do daryl.get('first'), I get undefined. daryl.get('id') works as expected. daryl.get('0') actually returns a plain old javascript object of the actual model, i.e. what I would probably expect to be my backbone model to ACTUALLY be. why is this?
Your server appears to be returning an array in its response, hence why calling model.get('0') is returning the attributes you really wanted. You either need to modify the server's response to only return the object (instead of an object inside an array) or you need to add a parse method to your model to return the first item in the response array.
Not sure if this is the issue in question (but doing console.log after calling fetch is problematic), but it is important to keep in mind that daryl.fetch()is happening asynchronously.
That is to say, you should try:
daryl.fetch().done(function(){
console.log(daryl.attributes);
model.get("first");
});
or
daryl.fetch({success : function(model){
console.log(model);
model.get("first");
}});
This ensures that the AJAX request was complete prior to trying to act on the model and very well maybe why get returns undefined.

Updating Chrome storage object key value

I'm creating a Google Chrome extension and I'm saving information using the chrome.storage.sync.set function. According to the API you can create an object and save the information between accounts. While I am not having any trouble creating this object, I am having trouble updating a specific key and syncing the value, without making an entirely separate object for each change.
For example my object looks something like this when logged to the console:
{
profile: {
preferences: {
username: 'my username'
}
}
}
I'd like to simply update the value 'username'.
I've tried doing something like this (I have access to the object through the chrome.storage.sync.set function callback):
_ext.profile.preferences.username = 'my new username';
This does update the object, but does not save and store it.
I have also tried this method:
_ext.profile.preferences.username = 'my new username 2'; /* update the key value */
chrome.storage.sync.set(_ext.profile) /* save the entire object to memory */
This method has not worked either.
What do you think is the problem here? Is it the way in which I'm trying to save the object or is there a better method to having a settings based approach?
If you are calling "get" right away, before the "set" has completed, that could be the problem. Your example does not show a callback being passed to handle completion of the "set".
I stumbled across your post while looking to solve the same issue. I ended up using a similar approach as React Redux state management. Instead of trying to manipulate the stored data, I make a copy then replace it.
var data = {};
chrome.storage.sync.get(function(result){
data = result.storedData;
data.profile.preferences.username = 'my new username';
});
chrome.storage.sync.set({'storedData': data});

Categories