How to get document _id from Meteor cursor? - meteor

I have rewritten this question as i now understand my problem a bit more. The answers below remain relevant.
I have the following query which returns a record.
Template.game.helpers({
Game: function () {
var myGame = Games.findOne(
{
game_minutes: {$gt: MinutesSinceMidnightNow},
court_id: court,
game_date: {$gt: lastMidnight}
},
{
sort: {game_minutes: 1}
}
); // find
console.log(myGame);
console.log(myGame._id);
return myGame;
} // game function
}); //template scoreboard.helpers
Meteor.startup(function () {
Meteor.call('removeGames', court, MinutesSinceMidnightNow);
for(var i=0;i<incomingGames.length;i++){
var game = incomingGames[i];
var gameTime = game.game_time;
if ( MinutesSinceMidnightGameTime(gameTime) > MinutesSinceMidnightNow ) {
console.log("game # " + i + ' game time ' + MinutesSinceMidnightGameTime(gameTime) + ' now' + ' ' + MinutesSinceMidnightNow);
Meteor.call('insertGame', game);
} // if
} // for
// game = Meteor.call("nextGame", MinutesSinceMidnightNow, court, lastMidnight);
console.log(MinutesSinceMidnightNow + ', ' + court + ', ' + lastMidnight);
}); // startup
The first console.log shows a game object which includes the _id property. The second console log throws an error. How can I get the _id value?
On thinking more about this, the code may actually work. Console log eventually displays nthe id number. The strange thing is the error occurs before the game inserts in server startup. I guess the client started before the server and then reactively aligned with the real data once the server started? This is hard to get my head around coming from traditional web development.
Here is the console output
undefined scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:118
Exception in template helper: TypeError: Cannot read property '_id' of undefined
at Object.Template.game.helpers.Game (http://localhost:3000/client/scoreboard/scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:122:19)
at http://localhost:3000/packages/blaze.js?88aac5d3c26b7576ac55bb3afc5324f465757709:2693:16
at http://localhost:3000/packages/blaze.js?88aac5d3c26b7576ac55bb3afc5324f465757709:1602:16
at Object.Spacebars.call (http://localhost:3000/packages/spacebars.js?3c496d2950151d744a8574297b46d2763a123bdf:169:18)
at Template.game.HTML.DIV.Spacebars.With.HTML.SPAN.class (http://localhost:3000/client/scoreboard/template.scoreboard.js?0ad2de4b00dfdc1e702345d82ba32c20d943ac63:16:22)
at null.<anonymous> (http://localhost:3000/packages/spacebars.js?3c496d2950151d744a8574297b46d2763a123bdf:261:18)
at http://localhost:3000/packages/blaze.js?88aac5d3c26b7576ac55bb3afc5324f465757709:1795:16
at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?88aac5d3c26b7576ac55bb3afc5324f465757709:2029:12)
at viewAutorun (http://localhost:3000/packages/blaze.js?88aac5d3c26b7576ac55bb3afc5324f465757709:1794:18)
at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36) debug.js:41
game # 0 game time 1395 now 549 scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:148
game # 1 game time 1110 now 549 scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:148
game # 2 game time 1185 now 549 scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:148
game # 3 game time 1260 now 549 scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:148
549, 1, Wed Oct 22 2014 00:00:00 GMT+0930 (CST) scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:157
Object {_id: "scYEdthygZFHgP2G9", court_id: 1, game_date: Wed Oct 22 2014 09:09:50 GMT+0930 (CST), court_name: "Court 1", game_time: "18:30"…} scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:118
scYEdthygZFHgP2G9

I cannot comment on the accepted answer, so I'll put the explaination as to why you see the log error here.
Your code runs just fine, the problem is (and reason for your log error) that you don't take into account that your collection of games isn't populated with any data yet. The first line in your log output reads:
undefined scoreboard.js?c19ff4a1d16ab47e5473a6e43694b3c42ec1cc22:118
which corresponds to
console.log(myGame);
The first time Meteor renders your templates, you simply don't have any data in the Games collection - it's on the wire on the way to your client. Meteor then automatically reruns your templates when data has arrived, explaining the subsequent console outputs.
So basically, the only thing that is wrong with your code at this moment, is the console log that tries to output the _id, since the during the first evaluation there is no game (thus you trying to access the property "_id" of the object "undefined" - the log error message). Remove that line and you should be ready to go!

If the parameter being passed to the function is an array, you can use Array.every. If it's a cursor, you'd need to fetch the results first.
UPDATE
I've just seen your comment. If you're looking for the first game after timenow, just do:
game = Games.findOne({game_minutes: {$gt: timenow}, [ANY OTHER FILTER]}, {sort: {game_minutes: 1}});
I've assumed the collection is called Games, and obviously you need to substitute in any other filter details to get the right set of games to look through, but it should work.

If you can access the game collection, I prefer adding selector and options to your query:
next_game = Games.find(
{
game_minutes: {$gt: timenow}
},
{
sort: {game_minutes: 1},
limit: 1
});
If not, fetch, filter, and then get the minimum one.
new_games = games.fetch().filter(function(game){
return game.game_minutes > timenow;
});
next_game = _.min(new_games, function(game){
return game.game_minutes;
});

Related

Select all/bulk messages

Is there a way or a script to select all/bulk messages in a channel/chat to be then forwarded?
The chat has like 4000 messages going back to 2019 so it's not feasable to do it manually
I've found this script online but it doesn't work, i'm not really into coding tbf, I just copy and paste.
http://shreekantranade.blogspot.com/2020/09/selecting-all-messages-from-telegram.html
function sel(cnt=200){$($(".im_history_selected_wrap .im_history_messages .im_history_messages_peer:not(.ng-hide) .im_message_outer_wrap").get().reverse()).each(function(e, el){if (e>cnt) return; el.click(); }); }
// bring all messages into view
lst=document.getElementsByClassName("im_history_scrollable_wrap");
ele=lst[0]
//ele.scrollTo(0,-100); // test
function scrl() { ele.scrollTo(0,-10000); }
var loop1 = setInterval(scrl, 1);
////clearInterval(loop1); //stop the loop once reached the top```

Access last value from a SignalProducer when terminated

I have a signal producer, when it's terminated I would like to know if a value was sent, I only need the last one, seems so simple ...
let myProducer: SignalProducer<MyObject, MyError> = getMyProducer()
myProducer.on(terminated: {
// I need the last value here
// Or I need to know if value was never called
}).start()
I've tried to store the value in a local var :
let myProducer: SignalProducer<MyObject, MyError> = getMyProducer()
var myValue: MyObject?
myProducer.on(value: { value in
myValue = value
}, terminated: {
guard let value = myValue else {
// value was never called
return
}
// value was called
}).start()
But sometimes terminated is called while value has been called but myValue is still nil...
First, are you really sure that you want the terminated event?
Under normal conditions, an event stream ends with a completed event. Exceptions are failed when a failure has occurred and interrupted, when the observation was ended before the stream could complete normally (E.g. cancellation).
Second: Can your SignalProducer fail, and in the failure case, do you still want the last value sent before the failure?
If not, its as easy as using the take(last:) operator:
enum MyError: Error {
case testError
}
let (signal, input) = Signal<Int, MyError>.pipe()
let observer = Signal<Int, MyError>.Observer(
value: { print("value: \($0)") },
failed: { print("error: \($0)") },
completed: { print("completed") },
interrupted: { print("interrupted") }
)
signal
.take(last: 1)
.observe(observer)
input.send(value: 1) // Nothing printed
input.send(value: 2) // Nothing printed
input.send(value: 3) // Nothing printed
input.sendCompleted() // value 3 printed
I'm using a Signal here so I can manually send events to it just for demonstration, the same works for SignalProducer as well.
Note: If we send interrupted or a failed event, the last value 3 will not be sent because those to terminating events short circuit the normal flow.
If your SignalProducer can fail, and you still want to get the last value before the failure, you can use flatMapError to ignore the Error before the last operator:
signal
.flatMapError { _ in
return .empty
}
.take(last: 1)
.observe(observer)
my answer :
producer
.flatMapError { _ in SignalProducer<Value, NoError>.empty }
.collect()
startWithResult( { result in
case let .success(results):
done(with: results.last)
case let .failure(error):
() // should not happen as errors are flatmapped
})

Display result of a query that calculates the total of a field as the value of a label in a list

I have two Datasource tables Projects and tt_records with a hours number field. There is a one to many relation between the Project and tt_records. I would like to display the total number of hours per project in a table. I am able to compute the total hours in server side function, how do I bind the total with a label on the UI. I am attempting to use the following in the binding on the field. I see the function is called through info statements in the console logs, however the value does not display on the UI clientgetProjHours(#datasource.item._key); following is the Client Script
function clientgetProjHours(key){
return (google.script.run.withSuccessHandler(function (key) {
console.info("received result");
}).getProjHours(key));
}
Following is the server side script
function getProjHours(key){
console.log ("In the function getProjHours (" + key +")");
var pRecords = app.models.Projects.getRecord(key);
console.log("Contents of " + pRecords);
var tRecords =pRecords.tt_record;
console.log("Contents of t Records" + tRecords);
var total = 0;
tRecords.forEach (function (item){
total += item.actuals;
});
console.log ("The result is: " + total);
return total;
}
Could you please suggest the best way to achieve this fuction.
Thank you very much for your help
key parameter in function (key) { is the result of the Server Script.
So you just need to replace:
function (key) {
With:
function (result)
Also replace:
console.info("received result");
With:
app.pages.[PageName].descendants.[LabelName].text = result;
But as it mentioned already Calculated Model should fit such use case better.

SQLite storage API Insert statement freezes entire firefox in bootstrapped(Restartless) AddOn

Data to be inserted has just two TEXT columns whose individual length don't even exceed 256.
I initially used executeSimpleSQL since I didn't need to get any results.
It worked for simulataneous inserts of upto 20K smoothly i.e. in the bakground no lag or freezing observed.
However, with 0.1 million I could see horrible freezing during insertion.
So, I tried these two,
Insert in chunks of 500 records - This didn't work well since even for 20K records it showed visible freezing. I didn't even try with 0.1million.
So, I decided to go async and used executeAsync alongwith Bind etc. This also shows visible freezing for just 20K records. This was the whole array being inserted and not in chunks.
var dirs = Cc["#mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
var dbFile = dirs.get("ProfD", Ci.nsIFile);
var dbService = Cc["#mozilla.org/storage/service;1"].
getService(Ci.mozIStorageService);
dbFile.append('mydatabase.sqlite');
var connectDB = dbService.openDatabase(dbFile);
let insertStatement = connectDB.createStatement('INSERT INTO my_table
(my_col_a,my_col_b) VALUES
(:myColumnA,:myColumnB)');
var arraybind = insertStatement.newBindingParamsArray();
for (let i = 0; i < my_data_array.length; i++) {
let params = arraybind.newBindingParams();
// Individual elements of array have csv
my_data_arrayTC = my_data_array[i].split(',');
params.bindByName("myColumnA", my_data_arrayTC[0]);
params.bindByName("myColumnA", my_data_arrayTC[1]);
arraybind.addParams(params);
}
insertStatement.bindParameters(arraybind);
insertStatement.executeAsync({
handleResult: function(aResult) {
console.log('Results are out');
},
handleError: function(aError) {
console.log("Error: " + aError.message);
},
handleCompletion: function(aReason) {
if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED)
console.log("Query canceled or aborted!");
console.log('We are done inserting');
}
});
connectDB.asyncClose(function() {
console.log('[INFO][Write Database] Async - plus domain data');
});
Also, I seem to get the async callbacks after a long time. Usually, executeSimpleSQL is way faster than this.If I use SQLite Manager Tool extension to open the DB immediately this is what I get ( as expected )
SQLiteManager: Error in opening file mydatabase.sqlite - either the file is encrypted or corrupt
Exception Name: NS_ERROR_STORAGE_BUSY
Exception Message: Component returned failure code: 0x80630001 (NS_ERROR_STORAGE_BUSY) [mozIStorageService.openUnsharedDatabase]
My primary objective was to dump data as big as 0.1 million + and then later on perform reads when needed.

Stream Heart Pulse from Apple Watch OS 2 [duplicate]

Can we access the heart rate directly from the apple watch? I know this is a duplicate question, but no one has asked this in like 5 months. I know you can access it from the Health App but I'm not sure how "real-time" that will be.
Heart Rate Raw Data information is now available in Watchkit for watchOS 2.0.
WatchOS 2 includes many enhancements to other existing frameworks such as HealthKit, enabling access to the health sensors that access heart rate and health information in real-time.
You could check this information in the following session which is total 30 minutes presentation.If you do not want to watch entire session, then you directly jump to Healthkit API features which is in between 25-28 min:
WatchKit for watchOS 2.0 Session in WWDC 2015
Here is the source code implementation link
As stated in the HKWorkout Class Reference:
The HKWorkout class is a concrete subclass of the HKSample class.
HealthKit uses workouts to track a wide range of activities. The
workout object not only stores summary information about the activity
(for example, duration, total distance, and total energy burned), it
also acts as a container for other samples. You can associate any
number of samples with a workout. In this way, you can add detailed
information relevant to the workout.
In that given link, the following part of the code defines sample rate of heartRate
NSMutableArray *samples = [NSMutableArray array];
HKQuantity *heartRateForInterval =
[HKQuantity quantityWithUnit:[HKUnit unitFromString:#"count/min"]
doubleValue:95.0];
HKQuantitySample *heartRateForIntervalSample =
[HKQuantitySample quantitySampleWithType:heartRateType
quantity:heartRateForInterval
startDate:intervals[0]
endDate:intervals[1]];
[samples addObject:heartRateForIntervalSample];
As they state there:
You need to fine tune the exact length of your associated samples
based on the type of workout and the needs of your app. Using 5 minute
intervals minimizes the amount of memory needed to store the workout ,
while still providing a general sense of the change in intensity over
the course of a long workout. Using 5 second intervals provides a
much-more detailed view of the workout, but requires considerably more
memory and processing.
After exploring HealthKit and WatchKit Extension, My findings are as follows:
We do not need the WatchKit Extension to get the Heart Rate Data.
You just need to have an iPhone with paired Apple watch (which is obvious)
The Default Apple Watch Heart Rate monitor app updates the HealthKit data immediately only when it is in the foreground.
When the Default Apple Watch Heart Rate monitor app is in the Background, it updates the HealthKit data at the interval of 9-10 mins.
To get the Heart rate data from the HealthKit following query needs to be fired periodically.
func getSamples() {
let heathStore = HKHealthStore()
let heartrate = HKQuantityType.quantityType(forIdentifier: .heartRate)
let sort: [NSSortDescriptor] = [
.init(key: HKSampleSortIdentifierStartDate, ascending: false)
]
let sampleQuery = HKSampleQuery(sampleType: heartrate!, predicate: nil, limit: 1, sortDescriptors: sort, resultsHandler: resultsHandler)
heathStore.execute(sampleQuery)
}
func resultsHandler(query: HKSampleQuery, results: [HKSample]?, error: Error?) {
guard error == nil else {
print("cant read heartRate data", error!)
return
}
guard let sample = results?.first as? HKQuantitySample else { return }
// let heartRateUnit: HKUnit = .init(from: "count/min")
// let doubleValue = sample.quantity.doubleValue(for: heartRateUnit)
print("heart rate is", sample)
}
Please update me if anyone gets more information.
Happy Coding.
Update
I've updated your code to be clear and general, and be aware that you need to get authorization for reading HeathKit data and adding info.plist key Privacy - Health Records Usage Description
There is no direct way to access any sensors on the Apple Watch. You will have to rely on access from HealthKit.
An Apple evangelist said this
It is not possible to create a heart monitor app at this time. The
data isn't guaranteed to be sent to iPhone in real-time, so you won't
be able to determine what's going on in any timely fashion.
See https://devforums.apple.com/message/1098855#1098855
You can get heart rate data by starting a workout and query heart rate data from healthkit.
Ask for premission for reading workout data.
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKQuantityType *type2 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
HKQuantityType *type3 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
[healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithObjects:type, type2, type3, nil] completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(#"health data request success");
}else{
NSLog(#"error %#", error);
}
}];
In AppDelegate on iPhone, respond this this request
-(void)applicationShouldRequestHealthAuthorization:(UIApplication *)application{
[healthStore handleAuthorizationForExtensionWithCompletion:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(#"phone recieved health kit request");
}
}];
}
Then implement Healthkit Delegate:
-(void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error{
NSLog(#"session error %#", error);
}
-(void)workoutSession:(HKWorkoutSession *)workoutSession didChangeToState:(HKWorkoutSessionState)toState fromState:(HKWorkoutSessionState)fromState date:(NSDate *)date{
dispatch_async(dispatch_get_main_queue(), ^{
switch (toState) {
case HKWorkoutSessionStateRunning:
//When workout state is running, we will excute updateHeartbeat
[self updateHeartbeat:date];
NSLog(#"started workout");
break;
default:
break;
}
});
}
Now it's time to write **[self updateHeartbeat:date]**
-(void)updateHeartbeat:(NSDate *)startDate{
//first, create a predicate and set the endDate and option to nil/none
NSPredicate *Predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:nil options:HKQueryOptionNone];
//Then we create a sample type which is HKQuantityTypeIdentifierHeartRate
HKSampleType *object = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
//ok, now, create a HKAnchoredObjectQuery with all the mess that we just created.
heartQuery = [[HKAnchoredObjectQuery alloc] initWithType:object predicate:Predicate anchor:0 limit:0 resultsHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *sampleObjects, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *newAnchor, NSError *error) {
if (!error && sampleObjects.count > 0) {
HKQuantitySample *sample = (HKQuantitySample *)[sampleObjects objectAtIndex:0];
HKQuantity *quantity = sample.quantity;
NSLog(#"%f", [quantity doubleValueForUnit:[HKUnit unitFromString:#"count/min"]]);
}else{
NSLog(#"query %#", error);
}
}];
//wait, it's not over yet, this is the update handler
[heartQuery setUpdateHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *SampleArray, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *Anchor, NSError *error) {
if (!error && SampleArray.count > 0) {
HKQuantitySample *sample = (HKQuantitySample *)[SampleArray objectAtIndex:0];
HKQuantity *quantity = sample.quantity;
NSLog(#"%f", [quantity doubleValueForUnit:[HKUnit unitFromString:#"count/min"]]);
}else{
NSLog(#"query %#", error);
}
}];
//now excute query and wait for the result showing up in the log. Yeah!
[healthStore executeQuery:heartQuery];
}
You also have a turn on Healthkit in capbilities. Leave a comment below if you have any questions.

Resources