Parse Server Cloud Code background job query and update objects - javascript

I have a Parse Server app hosted on back4app and I am running a Background Job that runs every minute and queries the Letters class, where the column deliveryDate is less or equal to the current date, here's my main.js file:
// DELIVER A LETTER
Parse.Cloud.job("deliverLetter", function (request, status) {
var now = new Date();
// var nowTime = date.getTime();
var Letters = Parse.Object.extend("Letters");
var query = new Parse.Query(Letters);
query.lessThanOrEqualTo("deliveryDate", now);
query.find().then (function (objects) {
objects.forEach(function (obj) {
obj.set("isDelivered", true);
Parse.Cloud.useMasterKey();
obj.save(null, { useMasterKey: true } ).then(function(obj) {
response.success(obj);
}, function(error) {
response.error(error)
});
});
});
So, for instance, I save a row in the Letters class where deliveryDate is set to yesterday, in order for me to test this Cloud Code function. There's another column called isDelivered and it's set to False. So, my function above should set isDelivered into True and update my Letters's object.
But it doesn't work, so I don't know what I'm doing wrong.
Edit
Thanks to danh, I've fixed my code as it follows:
var Letters = Parse.Object.extend("Letters");
var query = new Parse.Query(Letters);
query.lessThanOrEqualTo("deliveryDate", now);
query.equalTo("isDelivered", false);
query.find().then (function (objects) {
let savePromises = objects.map(function (obj) {
obj.set("isDelivered", true);
return obj.save(null, { useMasterKey: true } );
});
Promise.all(savePromises).then(function(obj) {
response.success(obj);
}, function(error) {
response.error(error)
});
});
I would need to call another function from my main.js file which sends a push notifications and needs some parameters. I usually call it from my app, how would I call it from within that above function?
Parse.Cloud.define("pushiOS", function(request, response) {
var user = request.user;
var params = request.params;
var userObjectID = params.userObjectID
var data = params.data
var recipientUser = new Parse.User();
recipientUser.id = userObjectID;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", userObjectID);
Parse.Push.send({
where: pushQuery,
data: data
}, { success: function() {
console.log("#### PUSH SENT!");
}, error: function(error) {
console.log("#### PUSH ERROR: " + error.message);
}, useMasterKey: true});
response.success('success');
});
Maybe into Promise.all()?
Promise.all(savePromises).then(function(obj) {
response.success(obj);
Parse.Cloud.define("pushiOS"...
}, function(error) {
response.error(error)
});

Any save() that's in progress or not yet started when response.success() is called will be terminated (or won't get a chance to start). To fix, collect promises for all of the saves, and run them together with Promise.all(), which resolves only after all of the promises passed to it have resolved.
query.find().then (function (objects) {
// Parse.Cloud.useMasterKey(); don't need this
let savePromises = objects.map(function (obj) {
obj.set("isDelivered", true);
return obj.save(null, { useMasterKey: true } );
});
Promise.all(savePromises).then(function(obj) {
response.success(obj);
}, function(error) {
response.error(error)
});
});
Also, note, incidentally, that query.lessThanOrEqualTo("deliveryDate", now); will get all of the objects with deliveryDates before now, including the ones you processed previously. That result will get monotonically longer over time, eventually exceeding the 1 minute between runs, or blowing some other system resource.
Maybe you really want...
query.lessThanOrEqualTo("deliveryDate", now);
query.equalTo("isDelivered", false);
EDIT
The second question can be handled by factoring a promise-returning push function like this...
function pushDataToUser(userID, data)
var recipientUser = new Parse.User();
recipientUser.id = userID;
let pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", userID);
return Parse.Push.send({ where:pushQuery, data:data });
}
That can be called from cloud code like this...
Parse.Cloud.define("pushiOS", function(request, response) {
let params = request.params;
pushDataToUser(params.userObjectID, params.data).then(function() {
console.log("#### PUSH SENT!");
response.success('success');
}, function(error) {
console.log("#### PUSH ERROR! " + JSON.stringify(error));
response.error(error);
});
}
...and the new promise-returning function can be added to any other promise chain like this...
// function doSomething() returns a promise (like from a find() or a save()
return doSomething().then(function() {
// initialize someId and someData
return pushDataToUser(someId, someData)
});

Related

Parse-Server: Unable to query objects

at the top of my index.html, I successfully initialize Parse-Server like this:
<script>
Parse.initialize("MY_KEY");
Parse.serverURL = 'http://MY_APP_NAME.herokuapp.com/parse/';
/*var GameScore = Parse.Object.extend("Test");
var gameScore = new GameScore();
gameScore.save({playerName: "Granto"}).then(function(object) {
alert("SUCCESS BRAHSKI");
});*/
</script>
I know the initialization is successful because I'm able to save a Test Object into the server as shown in the commented code above.
However, When I try and query objects from my server, Nothing happens. What is wrong with my query?
var query = new Parse.Query("Test");
query.find({
success: function (results) {
alert("Query SUCCESS");
},
error: function (error) {
alert("Query Error:" + error);
}
});
Correct Syntax for a Parse-Server Query:
var query = new Parse.Query("Client");
query.find()
.then(function(results) {
alert("SUCCESS");
})
.catch(function(error) {
alert("ERROR");
});

create a website uptime monitor in Node.js

I want to create a uptime monitor using NodeJS and MongoDB. I want to run a cron job in NodeJS and store the data into MongoDB. If the website response status code is not equal to 200 then it will be saved in the database. I want to make a database entry like this,
url : http://www.google.com
status_code : 500
start_time :- start time
end_time :- end time
I can run the cron job but not sure how to save the downtime in the database. As, I don't want to store every response into the database. Only when response status code is other than 200 , then it will start tracking (start_time) the URL and it keeps the time when website is back to 200 as end_time.
cron.js :-
var async=require('async');
const Entry = require('../models/health.model.js');
var https = require('https');
var request = require('request');
module.exports = function getHttpsRequests () {
Entry.find({},function(err,entrys){
console.log(err);
if(!err && entrys){
async.each(entrys,function(entry,callback){
request(entry.url, function (error, response, body) {
entry.statuscheck=response.statusCode;
entry.save();
callback();
});
},function (error) {
});
}
});
}
health.model.js :-
const mongoose = require('mongoose');
const EntrySchema = mongoose.Schema({
url: String,
statuscheck: String
}, {
timestamps: true
});
module.exports = mongoose.model('Entry', EntrySchema);
I would do something like this to handle updating the database. I went ahead and put standard arrow functions in, because it was easier for me that way. I put some comments in so that should clear most questions up. It may not be the most elegant solution because I wrote it in 5 minutes, but if you follow this general logic flow, you should be much closer to your solution (its completely untested mind you.)
var async=require('async');
const Entry = require('../models/health.model.js');
var https = require('https');
var request = require('request');
module.exports = function getHttpsRequests () {
Entry.find({}, (err,entrys) => {
console.log(err);
if (!err && entrys) {
async.each(entrys, (entry,callback) => {
request(entry.url, (error, response, body) => {
//first check if the url has a document in the db.
Entry.find({ url: entry.url }, (err, entry) => {
if(!entry) {
//since the document does not exist, check the statusCode.
if(response.statusCode===200) { //if the statusCode is 200, continue the loop.
callback();
} else { //if the status code is not 200, lets save this to the db.
console.log("Saving object: " + entry)
entry.status_code = response.statusCode;
entry.start_time = new Date();
entry.save();
callback();
}
} else if (entry) {
//since the document exists, lets check the statusCode.
if(response.statusCode===200) { //if the statusCode is 200, update the stop_time.
entry.end_time = new Date();
Entry.findOneAndUpdate({ url: entry.url }, entry, (err, object) => { //this returns the entry after update, so we can put that in the console for easy debug.
if (err) {
console.log(err);
callback();
} else {
console.log("Object saved: " + object);
callback();
}
});
}
} else { //there was an error finding the document in the db, just go to the next one.
callback();
});
});
});
}
});
}

Parse: ParseError { code: 101, message: 'Object not found.' }

I have a cloud function running some code like this and I am able to get a response for my query which is a valid class instance, but when I try to update the instance with the set method, I get the error you see in the title.
async function addToDB(apiKey) {
const query = new Parse.Query(MyClass);
query.equalTo('apiKey', apiKey);
const response = await query.find({ useMasterKey: true });
const myInstance = response[0];
myInstance.set('total', 100);
try {
await myInstance.save({ useMasterKey: true });
} catch (e) {
console.log('E', e);
}
}
the options parameter ( { useMasterKey : true}) should be the second parameter passed to save
the first parameter to a save should be a null, i.e. :
myInstance.save(null, { useMasterKey: true })
in essence, you are not passing the masterkey option in to the save call - which is why you are getting the 101 error (in my experience, a 101 is almost always related to permissions issues!)
see more here http://parseplatform.org/Parse-SDK-JS/api/v1.11.1/Parse.Object.html#save

AWS Lambda function processes same dynamodb stream multiple times. What am I missing?

I have written a node.js lambda function that triggers based on a dynamodb stream when new records are inserted into a particular table.
The function receives only new events, filters for inserted records, and then for each record, uses a couple of fields to retrieve data from other tables. Using this combined data a message is composed and sent via SNS to specific target ARN.
The function performs correctly. All the relevant data is retrieved, and a push notification is sent out.
However, for some reason the function appears to be called several times for the same stream, and processes the newly inserted records several times. The result is the target device receiving the same push notification several times.
Should I be placing the callback in a different place, or am I not calling on the context correctly?
This is the function:
'use strict';
var AWS = require("aws-sdk");
var dynamodb = new AWS.DynamoDB();
var sns = new AWS.SNS();
console.log('Loading function');
exports.handler = (event, context, callback) => {
console.log('Received event:', JSON.stringify(event, null, 2));
event.Records.forEach((record) => {
console.log(record.eventID);
console.log(record.eventName);
console.log('DynamoDB Record: %j', record.dynamodb);
if (record.eventName == 'INSERT') {
var matchId = record.dynamodb.NewImage.eventId.S;
var match_params = {
Key: {
"eventId": {
S: matchId
}
},
TableName: "xxxxxxxxxxx-mobilehub-xxxxxxx-Event"
};
//retrieve the match information from Event table
dynamodb.getItem(match_params, function(err, data) {
var match_description = "";
if (err) {
console.log(err, err.stack);
context.fail('No match event record found in Event table');
} else {
match_description = data.Item.description.S;
var uId = record.dynamodb.NewImage.participantUserId.S; //participantUserId
var user_params = {
Key: {
"userId": {
S: uId
}
},
TableName: "xxxxxxxxxxx-mobilehub-xxxxxxxxx-User"
};
//retrieve the user record from User table
dynamodb.getItem(user_params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
context.fail('Error occurred. See log.');
} else {
console.log(data); // successful response
if (data.length === 0) {
console.log("No User Record Found.");
context.fail('No user found for participantUserId.');
} else {
var deviceARN = data.Item.device_arn.S;
if (deviceARN <= 1) {
console.log("User has not registered their device for push notifications.");
context.fail('User has not registered for notifications');
} else {
var json_message = JSON.stringify({
APNS_SANDBOX: JSON.stringify({
aps: {
alert: "You are playing in an upcoming match " + match_description,
badge: 1,
sound: 'default'
}
})
});
var snsparams = {
Message: json_message,
MessageStructure: 'json',
TargetArn: deviceARN
};
sns.publish(snsparams, function(err, data) {
if (err) {
console.log(err); // an error occurred
context.fail('SNS send failed. See log.');
} else {
console.log(data); // successful response
context.success('Push notification sent to user.');
}
});
}
}
}
});
}
});
}
});
callback(null, `Successfully processed ${event.Records.length} records.`);
};
In my case, I added the same event source multiple times.
Quote from the conversation with an AWS support engineer:
Using my internal tools, I noticed that the Lambda function xxxxxx has
the event source:
arn:aws:events:my_region:my_acct_id:rule/my_event_target
configured twice as push event source. This means that this might be the cause
why you are seeing two invokes at every minute. Would you please
confirm on your side if this event is configured twice for the $LATEST
version of your lambda and also confirm if it's intended?
I hope this could save someelse :)
In your lambda page at the bottom, try tweaking "Concurrency" Unreserved account concurrency to 1 and "Asynchronous invocation" Retry attempts to 0 . As a test try these and observe the behaviour. Might help.

Parse.com Cloud code Error: success/error was not called when trying to update a user

ive never used cloud code/javascript and I am trying to write some parse cloud code to find a user using a objectId passed in to the cloud function, and then update that users relation that holds friends and finally save that user.
below is the function im using:
Parse.Cloud.define("addFriendToFriendsRelation", function(request, response) {
Parse.Cloud.useMasterKey();
var fromUserObjectId = request.params.fromUserObjectId;
var acceptingUser = request.params.user;
var query = new Parse.Query(Parse.User);
// find the user the request was from using the objectId
query.get(fromUserObjectId, {
success: function(user) {
var fromUser = user
var relation = fromUser.relation("friends");
relation.add(acceptingUser);
fromUser.save({
success: function() {
response.success("Successfully saved the users relation")
},
error: function() {
response.error("Save failed");
}
});
},
error: function() {
response.error("Save failed");
}
});
});
I managed to piece this together using the Parse docs. but Im really not following it to well. Never used javascript and am finding the syntax confusing.
then im calling the function with
//fromUser is a PFUser object defined further up
[PFCloud callFunctionInBackground:#"addFriendToFriendsRelation" withParameters:#{#"fromUserObjectId" : fromUser.objectId} block:^(id object, NSError *error) {
}
however whenever this function is called I get a success/error was not called error. Though im calling response.success and response.error in the function so I dont know why that is? Can anyone lend a hand?
edit: after doing some more searching it looks like response.success and response.error should only be called once each, so I modified my function to look like this:
arse.Cloud.define("addFriendToFriendsRelation", function(request, response) {
Parse.Cloud.useMasterKey();
var fromUserId = request.params.fromUserObjectId;
console.log("fromUserId:");
console.log(fromUserId);
var acceptingUser = request.params.user;
console.log("acceptingUser:")
console.log(acceptingUser);
var query = new Parse.Query(Parse.User);
query.get(fromUserId, {
success: function(user) {
console.log("found user:");
console.log(user);
var fromUser = user;
var relation = fromUser.relation("friends");
relation.add(acceptingUser);
console.log("added accepting user to relation");
fromUser.save({
success: function() {
response.success("successfully saved user")
},
error: function() {
response.error("error saving user");
}
});
console.log("found a user");
},
error: function() {
console.log("error finding user");
}
});
});
An old question, but since it's been up-voted, maybe answering can help someone else :).
First off, there is an error in how you are saving fromUser.
fromUser.save({ success: ...
If you look at the api you can see that it should be of the form:
fromUser.save(null, { success: ...
But the larger problem that kept you from finding your bug is that errors are getting eaten 'cause you are using the old style method of dealing with async code instead of using promises.
Below, I have re-written to use promises. Note:
I always return promise generating calls (there are other options for catching errors in async code, but start with this.)
Put a .catch at the end. The .catch is effectively the same things as .then(null, response.error) but either way, it is imperative that there is final backstop to catch errors. In your code above, the error was in a success block, that was running async, so when there was an error, it failed with no one to hear it :).
Parse.Cloud.define("addFriendToFriendsRelation", (request, response) => {
const fromUserId = request.params.fromUserObjectId;
console.log("fromUserId:", fromUserId);
const acceptingUser = request.user;
console.log("acceptingUser:", acceptingUser)
new Parse.Query(Parse.User);
.get(fromUserId, { useMasterKey: true })
.then((fromUser) => {
console.log("found fromUser:", fromUser);
const relation = fromUser.relation("friends");
relation.add(acceptingUser);
console.log("added accepting user to relation");
return fromUser.save(null, { useMasterKey: true })
})
.then(response.success)
.catch(response.error);
});

Categories

Resources