Parse Cloud Code Queries do not return anything ever - parse-platform

I have some parse cloud code im running on my self hosted server but im running into an issue where queries are not doing anything. I can run commands through terminal and get data back but when I run a query.find.. nothing happens. For Example:
Parse.Cloud.job("getall", function(request, response) {
var itemStatus = Parse.Object.extend('MovieStatus');
var query = new Parse.Query(itemStatus);
query.find({
success: function(results) {
console.log(results.length)
response.success(results.length);
},
error: function(err) {
response.error(err);
},
useMasterKey : true
})
})
Nothing happens. No error no response. I have added console logs to make sure its at least getting called and it is, but for some reason nothing every returns from the server when I do query.find
I have tried all sorts of things to figure out what the issue is but this affects all of my cloud code so it has to be something in there.

You are using an old syntax. Since version 3.0, Parse Server supports async/await style. Try this:
Parse.Cloud.job("getall", async request => {
​const { log, message } = request;
const ItemStatus = Parse.Object.extend('MovieStatus');
const query = new Parse.Query(ItemStatus);
const results = await query.find({ useMasterKey: true });
log(response.length);
message(response.length);
})
Not this is a job and not a cloud code function. You can invoke this job using Parse Dashboard and you should see the message in the job status section.

Related

AWS Textract does not run callback in error free run of analyzeDocument

CURRENTLY
I am trying to get AWS Textract working on a Lambda function and am following documentation on https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Textract.html#analyzeDocument-property
My Lambda code:
"use strict";
const AWS = require("aws-sdk");
exports.handler = async (event) => {
let params = JSON.parse(event.body);
console.log("Parse as document...");
let textract = new AWS.Textract();
let doc = params["doc"];
let config = {
Document: {
Bytes: doc,
}
};
textract.analyzeDocument(config, function (err, data) {
console.log("analyzing..."); //<-- nothing logged to console if no error
if (err) {
console.log(err, err.stack);
}
// an error occurred
else {
console.log("data:" + JSON.stringfy(data)); //<-- nothing logged to console if no error
} // successful response
});
console.log("Finished parsing as document.");
};
ISSUE
I cannot get the data back from Textract. It seems I am unable to get the callback working entirely. What's odd is if there is an error e.g. my configuration is wrong, the error handling of the callback will print the log and "analyzing..." log, but without error, none of the logs in the callback print.
Current Logs:
Parse as document...
Finished parsing as document.
Expected / Desired Logs:
Parse as document...
analyzing...
data:{textract output}
Finished parsing as document.
Please help!
NOTES
I am using a role for that Lambda that allows it to access Textract.
I get the same result whether I include the HumanLoopConfig settings or not.
Solved, apparently I needed to setup a promise:
let data = await textract.analyzeDocument(config).promise()
console.log("data:"+data );
console.log("Finished parsing as document.")

How to update an User with useMasterKey in Parse

Issue Description
I'm trying to update an User when another user click on my Xamarin button.
Then, I used Cloud Code to perform this but it doesnt work
My Code
Here is my complete JS code :
Parse.Cloud.beforeSave("Archive", function(request, response) {
Parse.serverURL = 'https://pg-app-0brffxkawi8lqvf2eyc2isqrs66zsu.scalabl.cloud/1/';
var status = request.object.get("status");
if (status == "validated") {
var event = request.object.get("event");
event.fetch({
success: function(myEvent) {
var coinsEvent = myEvent.get("coins");
var user = request.object.get("user");
user.fetch({
success: function(myUser, coinsEvent, user) {
var email = myUser.get("email");
var coinsUser = myUser.get("coins");
myUser.set("coins", coinsUser + coinsEvent);
return myUser.save(null, {useMasterKey:true});
}
});
}
});
}
response.success();
});
I think myUser.save(null, {useMasterKey:true}); should work
I actually have that error :
Dec 24, 2017, 12:27 GMT+1 - ERRORError generating response for [PUT] /1/classes/_User/1GPcqmn6Hd
"Cannot modify user 1GPcqmn6Hd."
{
"coins": 250
}
Environment Setup
Server
parse-server version : v2.3.3
Server: Sashido
Your success branch never calls response.success() which is a problem... though maybe not THE problem.
You are also doing 2 fetches inside a 'beforeSave' function which is not recommended. 'BeforeSave' must happen very quickly and fetches take time. I would consider thinking through other options.
If you really need to do it this way, consider doing a Parse.Query("event") with an include("user") and trigger the query with query.first({useMasterKey:true}).
Are you sure coinsEvent is what you think it is? Fetch only returns the object fetched... not sure that you can curry in other parameters. I would change your final success routine to (double checking that coinsEvent is valid):
success: function(myUser) {
var coinsUser = myUser.get("coins");
myUser.set("coins", coinsUser + coinsEvent);
return myUser.save(null, {useMasterKey:true}).then(_ => response.success());
}

Parse Cloud Code query causing "No such app" error

I'm running Parse Server on heroku and my cloud code functions work except when I try to query.
Parse.Cloud.define('debuggingFn', function(request, response) {
var query = new Parse.Query("Speech");
query.equalTo("speechId", "s_1456277936842");
query.find({
success: function(results) {
console.log('SUCCESS', results)
response.success("Success", results);
},
error: function(a, b) {
// ERROR CAUGHT HERE: 'Heroku | No such app'
console.log('ERROR', a, b)
response.error("Error");
}
});
});
Speech is a valid class and that speechId exists. I don't know what I'm missing?
Found the issue. My SERVER_URL config field in heroku had a typo in it. Fixed that and everything seems to work now :)

Proper separation of concerns natively in node.js?

I am a total node.js noobie and trying to figure out the best way to structure my application with proper separation of concerns.
I am using mongodb via mongoose and have successfully gotten my controllers separated out using node.js modules and am trying to then separate out my models. What I've gone appears to work, but when I check the database nothing has been saved. Also, I tried a console.log() in the save function and nothing gets logged.
from my server.js I have:
app.post(api.urlslug + '/messages', messagesapi.insert);
I then have a /controllers/api/messages.js:
var m = require('../../models/message');
exports.index = function(req, res, next){
res.send('all the messages...');
}
exports.insert = function(req, res, next){
var message;
message = new m.Message({
messagebody: req.body.messagebody
});
message.save(function(err) {
console.log('here we are in the save');
if(!err) {
return console.log('message created');
} else {
return console.log(err);
}
});
return res.send(message);
}
and my /models/message.js looks like this:
// required modules
var mongoose = require('mongoose')
, db = require('../models/db');
// setup database connection
mongoose.connect(db.connectionstring());
var Message = exports.Message = mongoose.model('Message', new mongoose.Schema({
messagebody: String
}));
When I post to the API I get a the proper JSON back and it even has the _id field with what appears to me as a mongodb provided unique id. With that, I am having trouble understanding why it is not actually going into mongodb if it appears to be creating the object and communicating with mongodb correctly.
sounds like a connection is not being made. try listening to the open/error events of the default mongoose connection to find out why.
function log (msg) { console.log('connection: ', msg) }
mongoose.connection.on('open', log);
mongoose.connection.on('error', log);

Node.js Express mongoose query find

I have a little problem with Express and mongoose using Node.js . I pasted the code in pastebin, for a better visibility.
Here is the app.js: http://pastebin.com/FRAFzvjR
Here is the routes/index.js: http://pastebin.com/gDgBXSy6
Since the db.js isn't big, I post it here:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
module.exports = function () {
mongoose.connect('mongodb://localhost/test',
function(err) {
if (err) { throw err; }
}
);
};
var User = new Schema({
username: {type: String, index: { unique: true }},
mdp: String
});
module.exports = mongoose.model('User', User);
As you can see, I used the console.log to debug my app, and I found that, in routes/index.js, only the a appeared. That's weird, it's as if the script stopped (or continue without any response) when
userModel.findOne({username: req.body.username}, function(err, data)
is tried.
Any idea?
You never connect to your database. Your connect method is within the db.export, but that is never called as a function from your app.
Also, you are overwriting your module.exports - if you want multiple functions/classes to be exported, you must add them as different properties of the module.export object. ie.:
module.export.truthy = function() { return true; }
module.export.falsy = function() { return false; }
When you then require that module, you must call the function (trueFalse.truthy();) in order to get the value. Since you never execute the function to connect to your database, you are not recieveing any data.
A couple of things real quick.
Make sure you're on the latest mongoose (2.5.3). Update your package.json and run npm update.
Try doing a console.log(augments) before your if (err). It's possible that an error is happening.
Are you sure you're really connecting to the database? Try explicitly connecting at the top of your file (just for testing) mongoose.connect('mongodb://localhost/my_database');
I'll update if I get any other ideas.

Resources