Parse nested queries: inner query never excutes - javascript

I'm trying to loop all buses and update their information by querying another class called bus stop. What I did is something like this:
busQuery.each(function(bus){
var busRouteQuery = new Parse.Query("busRoute");
busRouteQuery.find({
//retrieve data from bus stop and update bus location
success: function(results){
},
error: function(){
}
});
});
However, the inner busRouteQuery is never called. any ideas?

I'm guessing you might be using an old version of the JavaScript SDK for Parse. Is this cloud code? From the guide:
"The default Parse JavaScript SDK version that is used for the Cloud Code in this directory is the latest version at the time the new command was run for this directory. If you want to change this, you can change the parseVersion in config/global.json."
https://parse.com/docs/cloud_code_guide#clt-version

Related

You do not have permission to use copyTo

I'm trying to copy a range from one sheet to another (whilst preserving the formulas). I wrote a simple script using copyTo:
function copyRangeAcrossSheets(source_sheet,source_range,target_sheet,target_range) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = spreadsheet.getSheetByName(source_sheet);
var target_sheet = spreadsheet.getSheetByName(target_sheet);
var source_range = source_sheet.getRange(source_range);
var target_range = target_sheet.getRange(target_range);
source_range.copyTo(target_range);
}
Which I call as followed:
=copyRangeAcrossSheets("TEST_source","A1:A3","TEST_target","A1:A3")
And I'm getting the below error:
You do not have the permission to call copyTo
I did some digging around and found that functions have to use special triggers (installable) in order to modify another file. However here I'm modifying the same file.
Q1: Why is copyTo failing here?
Q2: How can I workaround the issue without having to define installable triggers? (I just want to copy ranges whilst preserving formulas)
Why is it failing?
You cannot modify other any documents that require authorization via a custom function. The reason for this is that your function is executed as an anonymous user, which cannot obtain the necessary authorization to edit other sheets or documents of yours.
Reference: https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services
Specific to you is this snippet:
Spreadsheet: Read only (can use most get*() methods, but not set*()).
Cannot open other spreadsheets (SpreadsheetApp.openById() or SpreadsheetApp.openByUrl()).
Also:
If your custom function throws the error message "You do not have permission to call X service.", the service requires user authorization and thus cannot be used in a custom function.
How can you work around this?
Write an Apps Script function that is executed via a trigger or manually, you can use onEdit or onChange triggers, or a time-based trigger. You can even manually run the function in the Apps Script IDE when you need to. This is the intended behavior of Apps Script.
Not sure about whether or not your data is persistent in your source spreadsheet, but you could always use the built-in IMPORTRANGE() function. Syntax is:
=IMPORTRANGE("SPREADSHEET_ID","SOURCE_SHEET!RANGE_START:RANGE_END")
Where SPREADSHEET_ID is the ID of the file you're working on.

What order do Google Chrome Sync Storage 'Get' Commands run?

I am having a very difficult time getting the timing of a simple counting process down.
When I use console.log and alerts to debug, it seems that the code runs randomly - not in the order it appears in the function.
I'm creating a Google Extension and using sync storage to store settings and retrieve them to update values for the user.
For instance, I'm trying to stuff a string into "localBody" so I can then count the words in the string.
So first up in background.js is a listener that fires when a specific sync variable is changed...and it pulls a value from sync storage:
chrome.storage.onChanged.addListener(function(changes, sync) {if (changes["Clabel"]){alert("Label Changed - start listener update...");
//Get body, label, group from Sync Storage
chrome.storage.sync.get('Cbody', function (results) {
localBody = results.Cbody;
console.log("first pulling body string from sync storage" + localBody);
});
Later in the code localBody is sent off to a sub that counts words in the string:
wordCount = wordCount + countWords(localBody); //Update total word count with latest addition
This code fails ("extensions::uncaught_exception_handler:8 Error in event handler for storage.onChanged: TypeError: Cannot read property 'replace' of undefined") and the console shows that it runs BEFORE the sync storage "get" command.
I have tried to do this basic task a zillion ways and I continually run into the unpredictability of pulling from sync storage. I don't know how to get around this - I need to pull a value and modify it, then stuff it back into sync storage.
Okay, for the bozos like me who are blundering along trying to build Google Extensions with only cursory knowledge of javascript...
The "Get" process is asynchronous. So you need to use callbacks to control when the value you want will be available.
This is a good explanation - I used the example code verbatim:
Chrome.storage.sync.get not storing value in local variable

Cloud code not working

i am new at parse..trying code..trying to run a trigger required in my project.but not able to track not even i am getting any error.
i am using cloud code i.e triggers...
what i want to do is, after update or save i want to run a trigger which will a column in a class with value of 200.
Parse.initialize('APPLICATION_ID', 'JAVASCRIPT_KEY');
Parse.Cloud.afterSave("match_status", function(request)
{
var query = new Parse.Query('Wallet');
query.set("wallet_coines_number", 200);
query.equalTo("objectId", "FrbLo6v5ux");
query.save();
});
i am using afterSave trigger in which match_status is my trigger name. after that i making a object called query of Wallet class. This object will set column 'wallet_coines_number' with the value 200 where objectId is FrbLo6v5ux. after that i used save function which will execute query.
Please guide me if i am wrong, or following wrong approach.
Thank You !
Have you read the Parse documentation on Cloud Code ?
The first line of your code is only relevant when you are initialising Parse JavaScript SDK in a web page, you do not need to initialise anything in Parse cloud code in the main.js file. Also you cannot use a query to save/update an object. A query is for searching/finding objects, when you want to save or update an object you need to create a Parse.Object and save that.
So you code should become something like:
Parse.Cloud.afterSave("match_status", function(request) {
var wallet = new Parse.Object('Wallet');
wallet.set("wallet_coines_number", 200);
wallet.set("objectId", "FrbLo6v5ux");
wallet.save();
});

Using AngularJS to process custom localStorage data

I wrote a bookmarklet that retrieves information from a page and stores it in JSON format in local storage (converting it to a string first, of course).
I would like a web app I am writing to be able to process this data, on the fly, preferably as it gets saved to the localStorage.
Right now i can change the item in LS via the console and refresh the page and the new data appears but I would like it to be live and seamless.
Any advice on how to go about this? I found several localStorage modules for angularJS and I tried them but they don't seem to allow me to retrieve from LS if the data is already there in LS.
In response to answer:
$scope.$watch(
function(){
return $window.localStorage.getItem('TestData');
},
function(newValueInStorage){
$scope.testingLS = newValueInStorage;
}
)
I tried this and I still get the data displayed by just doing a {{ testingLS }} in the view template but when I go and change the TestData key in local storage via the console it doesn't update instantly. (for now, I am just testing it without the bookmarklet with just a simple string inside TestData
There is few ways to do it
One of will be to populate correct model on scope when saving to localStorage
The other that I can think of at this moment is to setup watcher
$watch(
function(){
return localstorage object
},
function(newValueInStorage){
$scope.modelFromLS = JSON.parse(newValueInsStorage)
}
)
---edit---
as per James comment you need something that will handle the fact that data has changed in different tab and $digest process need to run for watch to be recalculated
http://plnkr.co/edit/zlS3wL65meBeA8KkV5KH?p=preview
window.addEventListener('focus', function(){
console.log('focus')
$scope.$digest()
})

Unable to update an entry in the firebase list

After few attempts, I am still unable to find the right way to update a list in firebase.
I have my dev account in firebase. But,just for this question I used the sample on the public firebase url:
var url1= new Firebase("https://samplechat.firebaseio-demo.com/message_list");
url1.push({text:'Its working',user_id:'123'});
setTimeout(function (){
url1.on('child_added', function(snap) {
snap.update({text:'Its not working',user_id:'123'});
snap.child('text').update('Its not working');
console.log('messages in range', snap.val());}
)},6000);
I am able to set the data, but when the child_added function is triggered for the messages. I am getting the below javascript error:
Uncaught TypeError: undefined is not a function
What can be the issue ?
You aren't using the data snapshot object correctly within your function.
https://www.firebase.com/docs/javascript/datasnapshot/index.html
Change it to
snap.ref().update({text: '....', user_id: '...'});
If you were just looking to get the value, you'd use
snap.val()
please provide more information on what you're trying to accomplish. Right now you're trying to update your snapshot(snap.update). I'm not sure what that's for. Instead of calling your snap, you can call your reference, url1.update. Also, take another look at the docs for reading data, https://www.firebase.com/docs/reading-data.html and writing data, https://www.firebase.com/docs/writing-data.html. Looks like you've combined functions from reading and writing data.

Categories

Resources