I want to solution for nested queries.
Actually, i want to get all reviews for each movie.
I have two tables:
Movie:
Review:
It's possible to calculate rating for each movie and set it to avgRating?
I don't know why, but i can't get all reviews for one movie.
Parse.Cloud.define("setAvg", function(request, response) {
var Movie = Parse.Object.extend("Movie");
var MovieReview = Parse.Object.extend("MovieReview");
var query = new Parse.Query(Movie);
query.each(function(movie){
var reviewQuery = new Parse.Query(MovieReview);
reviewQuery.equalTo("relatedMovie", movie);
reviewQuery.find({
success: function(reviews){
console.log(reviews);
}
});
}).then(function() {
response.success("Migration completed successfully.");
}, function(error) {
response.error("Uh oh, something went wrong.");
});
});
I getting in logs:
I2015-12-21T17:34:57.777Z][]
I2015-12-21T17:34:57.778Z][{}]
I2015-12-21T17:34:57.779Z][{},{}]
I2015-12-21T17:34:57.780Z][{},{}]
I2015-12-21T17:34:57.781Z][{},{},{}]
I2015-12-21T17:34:57.782Z][{},{}]
Parse.Cloud.define("setAvg", function(request, response) {
var Movie = Parse.Object.extend("Movie");
var MovieReview = Parse.Object.extend("MovieReview");
var reviewQuery = new Parse.Query(MovieReview);
var query = new Parse.Query(Movie);
query.equalTo("relatedMovie",reviewQuery);
query.find(function(movie){
//do whatever you want with the query results
});
});
I can see several potential issues :
You should use a background job instead of a function to ensure you have enough time
You should define the reviewQuery inside the each block to avoid mixing
If you can have more than 100 ratings for one movie, you should set the limit of query to 1000 (which is the maximum limit - if there could be more than 1000, you will have to make the same query several times while incrementing the skip parameter).
The solution was as simple as possible.
Thank all for answers.
Parse.Cloud.job("setMovieRating", function(request, response) {
Parse.Cloud.useMasterKey();
var Movie = Parse.Object.extend("Movie");
var MovieReview = Parse.Object.extend("MovieReview");
var query = new Parse.Query(Movie);
query.each(function(movie){
var reviewQuery = new Parse.Query(MovieReview);
reviewQuery.equalTo("relatedMovie", movie);
return reviewQuery.find().then(function(reviews){
var avgRating = 0;
for (var i=0; i<reviews.length;i++){
avgRating+=reviews[i].get('starRating');
}
avgRating = avgRating/reviews.length;
var floatRating = Math.floor(avgRating * 100) / 100;
movie.set('avgRating', parseFloat(floatRating.toFixed(1)));
console.log(movie);
movie.save();
});
}).then(function() {
response.success("Success");
}, function(error) {
response.error("Uh oh, something went wrong.");
});
});
Related
router.post("/cart/paycash/add-order",(req,res) => {
req.checkBody("emri","Ju lutem vendosni emrin").notEmpty();
req.checkBody("mbiemri","Ju lutem vendosni mbiemrin").notEmpty();
req.checkBody("numritelefonit","Ju lutem vendosni numrin e telefonit").notEmpty();
req.checkBody("qyteti","Ju lutem vendosni qytetin").notEmpty();
var emri = req.body.emri;
var mbiemri = req.body.mbiemri;
var telefoni = req.body.numritelefonit;
var email = req.body.email;
var qyteti = req.body.qyteti;
var adresa = req.body.adresa;
var Cart = req.session.cart;
var errors = req.validationErrors();
if(errors) {
res.redirect("/cart/checkout", {
errors:errors
});
}
else {
Orders.find({}, function(err,orders) {
if(err) {
console.log(err);
}
else {
var order=new Orders({
emri:emri,
mbiemri:mbiemri,
telefoni:telefoni,
email:email,
qyteti:qyteti,
adresa:adresa,
});
console.log(Cart.length);
Cart.forEach(function(product) {
var cart = Cart.length;
var productTitle = product.title;
console.log(productTitle);
for (var i = 0; i < 1; i++) {
Products.findOne({title:product.title}, function(err,foundproduct) {
console.log(foundproduct.title)
order.products.push(foundproduct);
order.save();;
});
}
});
}
});
delete req.session.cart;
delete req.session.promocode;
res.redirect("/dyqani");
}
});
I want to make this code works, but for this I need async. I have tried some methods but I couldn't made it work. Can anyone help me? I want to be able to add in my order database all the products that are in the cart, but because mongoose is async and JavaScript is not, some of the queries get loaded before and the results in my database are not the ones that are in my cart.
It seems like you're trying to asynchronously request for every product that's in your cart. You don't need the nested for loop in your forEach function, because that loop doesn't seem to do anything.
You need to first map all the async requests in an array and run Promise.all to request them all asynchronously
let products = Cart.map((product) => {
return Products.findOne({title:product.title},(err,foundproduct) => {
//your save product to order logic
});
})
Promise.all(products).then((complete) => {
console.log('product added')
})
Here's a detailed explanation on how promises work in javascript Javascript Promises
Your application design pattern is also uncommon, but that's outside the scope of this question.
Parse-server doesn't support groupBy for queries. So instead of adapting code to work with the duplicate entries i've decided to create a Job to clean the data.
I've created a cloud function using underscore but the results are not good. It's deleting non-duplicate entries too.
I want to remove a entry if another entry exists with the same post_id and user_id
Parse.Cloud.job("removeDuplicateItems", function(request, status) {
var _ = require("underscore");
var hashTable = {};
function hashKeyForTestItem(testItem) {
var fields = ["user_id", "post_id"];
var hashKey = "";
_.each(fields, function (field) {
hashKey += testItem.get(field) + "/" ;
});
return hashKey;
}
var testItemsQuery = new Parse.Query("Post_shares");
testItemsQuery.each(function (testItem) {
var key = hashKeyForTestItem(testItem);
if (key in hashTable) { // this item was seen before, so destroy this
return testItem.destroy();
} else { // it is not in the hashTable, so keep it
hashTable[key] = 1;
}
}).then(function() {
status.success("removal completed successfully.");
}, function(error) {
status.error("Uh oh, something went wrong.");
});
});
Is there a better way of doing this?
I'm struggling to make this works. i'm dealing with sequelize promisses and i want to return the queried elements to the view, but instead its returning null. I know its because promises are async requests and it does not return a result immediatly after you call it, ok, but how to return the values, put it into an array and than return the array?
this is the code i have so far.
router.post('/register', function(req, res, next) {
var sales = req.body.sales;
var person = req.body.personID;
var promisses = [];
var delivery = req.body.delivery;
for(var i =0;i<sales.length;i++){
var product_id = sales[i].product_id;
var amount = sales[i].amount;
var price = sales[i].produto_price;
var salePromisse = Sale.create({
'product_id': product_id,
'person_id': person,
'amount': amount,
'price': price,
'total': amount*price
});
//i couldnt find a word which describes what movimentacao means...lets keep it.
var movimentacao = Movimentacao.create({
"tipo": 0,
"id_product": product_id,
"id_provider": "",
"data": new Date(),
"price": amount*price
});
promisses.push(salePromisse);
promisses.push(movimentacPromisse);
}
Promise.all(promisses).then(function (promissesArray){
var name = "";
var suc = 0;
var total = 0;
var salesResult = [];
var salesObject = {};
if(!delivery){
res.send({msg: "aaaaa"});
}else{
promissesArray.forEach(function(pro){
if(pro!==null && pro !== undefined){
if(pro['$modelOptions'].tableName === undefined){
Product.findById(pro.product_id).then(function (product){
salesObject.product = product.name;
salesObject.price= pro.price;
salesObject.amount = pro.amount;
salesObject.total = pro.total;
total += salesObject.total;
salesResult.push(salesObject);
salesObject = {};
return Person.findById(per);
}).then(function (person) {
name = person.name;
});;
}
}
});
//here is where i would like to return to the view salesResult and name.
//res.render("folder/view", {sales: salesResult, person: name});
console.log(salesResult);
}
});
});
Well, the promisses array has the CREATE instance for each of my models, and i'm creating a few types of it.
I want to insert on the database, check if the promise resolved is dealing with an specific table (the field modelOptions is undefined on it, i already debugged), query all the other results because on the promisses array i have just the id, and than put into the array to be able to return to the view, but on the console.log on the last line, it returns null. How can i improve the code to be able to do all i want and than return to the view?
Dont worry, all the model related variables are beeing declared above.
Thanks.
I'm making a quiz. I'm loading in the questions from a json file. The models have a 'use: ' attribute. I'm filtering the collection by use and plucking the IDs, then I'm getting a random ID for the question and setting use to false once it has been answered. In this way, a user will go through all the questions in a random order with no repeats. I'd like to keep track of the used questions for each user and when they have done them all reset the question bank.
I'm using backbone.dualstorage and it worked once and then I tried to reset the collection by going through each model and destroying it. Now I can't seem to repopulate the local collection with models because the remote models have the same IDs as the destroyed model IDs.
How can I re-add all the models in the remote collection to localStorage again?
//Question collection
var PlayCollection = Backbone.Collection.extend({
model: PlayModel,
url: "https://raw.githubusercontent.com/robertocarroll/barjeel-app/master/app/data/questions.json",
//when I set remote it works without local at all
//remote: function () {return true;}
});
//define new question collection
var newCollection = new PlayCollection();
// load data
newCollection.fetch({
success: function (newCollection, response, options) {
console.log("fetch questions success");
//this shows all the question IDs which I destroyed
console.log(newCollection.destroyedModelIds());
//this is empty
console.log(newCollection.dirtyModels());
}
});
function getQuestion() {
var theQuestions = newCollection.dirtyModels()[0].collection;
//get the IDs of all questions which haven't been used
var questions = theQuestions.chain()
.filter(function (m) {
return m.get('use')
})
.pluck('id')
.value();
console.log(questions);
if (questions.length > 0) {
// get random ID from question ID array
var rand = questions[_.random(questions.length - 1)];
console.log('chosen ID value: ' + rand);
//get a model from a collection, specified by ID
var currentQuestion = theQuestions.get(rand);
console.log(currentQuestion);
//set the status of that question to used
currentQuestion.set("use", false);
}
//if there's not more questions
else {
console.log("No more questions");
//delete models in local
_.chain(newCollection.models).clone().each(function (model) {
console.log('deleting model ' + model.id);
model.destroy();
});
}
}
Here's the fiddle: http://jsfiddle.net/robertocarroll/10xqvk18/10/
Thanks!
Turns out backbone.dualstorage was overkill. I accessed localStorage directly instead:
//model for questions
PlayModel = Backbone.Model.extend({});
//model for which questions to use
PlayGameQuestionCount = Backbone.Model.extend({
defaults: {
"use": true
}
});
//Question collection
var PlayCollection = Backbone.Collection.extend({
model: PlayModel,
url: "https://raw.githubusercontent.com/robertocarroll/barjeel-app/master/app/data/questions.json"
});
//define new question collection
var newCollection = new PlayCollection();
function fetchQuestions() {
// load data
newCollection.fetch({
success: function (newCollection, response, options) {
console.log("fetch questions success");
//add data to local storage
localStorage.setItem('questions', JSON.stringify(newCollection.toJSON()));
}
});
}
function getQuestion() {
var newLocalCollection = new PlayCollection(JSON.parse(localStorage.getItem('questions')));
console.log(newLocalCollection);
//get the IDs of all questions which haven't been used
var questions = newLocalCollection.chain()
.filter(function (m) {
return m.get('use')
})
.pluck('id')
.value();
console.log(questions);
if (questions.length > 0) {
// get random ID from question ID array
var rand = questions[_.random(questions.length - 1)];
console.log('chosen ID value: ' + rand);
//get a model from a collection, specified by ID
var currentQuestion = newLocalCollection.get(rand);
console.log(currentQuestion);
//set the status of that question to used
currentQuestion.set("use", false);
localStorage.setItem('questions', JSON.stringify(newLocalCollection.toJSON()));
}
//if there's not more questions
else {
console.log("No more questions");
//delete models in local
fetchQuestions();
}
}
//function to fire the questions
$(document).ready(function () {
$("#btnSave").click(
function () {
getQuestion();
});
});
Here's the fiddle: http://jsfiddle.net/hcqse7j4/3/
I'm trying to set up a game that allows playing with random players. The code below is supposed to create a GameMessage object for both paired players. To relate both objects as part of the same game, I've decided to save the objectId of of the game made for "firstplayer" in the field "otherside" for "secondplayer" and vice-versa. For some reason (perhaps the first save of firstplayer and secondplayer isn't done before the code attempts to retrieve the objectIds, meaning there are no objectIds to get?).
Short version: Why are the "otherside" values not saving?
Parse.Cloud.define("findpartner", function(request, response) {
var User = Parse.Object.extend("_User");
var user = new User();
var currentuser = Parse.User.current();
currentuser.set("searching", 0);
var query = new Parse.Query(User);
query.equalTo("searching", 1);
query.limit(50); //limit to at most 50 users
query.find({
success: function(objects) {
var amount = objects.length;
var indexNum = Math.floor((Math.random() * amount));
var newpartner = objects[indexNum];
if (amount > 0 && newpartner.id !=currentuser.id) {
newpartner.set("searching", 0);
var Firstplayer = Parse.Object.extend("GameMessages");
var firstplayer = new Firstplayer();
var Secondplayer = Parse.Object.extend("GameMessages");
var secondplayer = new Secondplayer();
firstplayer.set("sender", currentuser.id);
firstplayer.set("receiver", newpartner.id);
firstplayer.set("sent",0);
firstplayer.set("received",0);
firstplayer.set("receiverName", newpartner.getUsername());
secondplayer.set("sender", newpartner.id);
secondplayer.set("receiver", currentuser.id);
secondplayer.set("sent",0);
secondplayer.set("received",0);
secondplayer.set("receiverName", currentuser.getUsername());
firstplayer.save().then(function(secondplayer){ <<<
return secondplayer.save(); <<<
}).then(function(firstplayer_update) { <<<
return firstplayer.save({ otherside: secondplayer.id}); <<<
}).then(function(secondplayer_update){ <<<
return secondplayer.save({ otherside: firstplayer.id}); <<<
});
newpartner.save(null, {useMasterKey: true});
}
else {
currentuser.set("searching", 1);
}
currentuser.save();
response.success(amount);
},
error: function(error) {
alert("Error: " + error.code = " " + error.message);
}
});
});
I added arrows to show where the "otherside" is. They're not in the actual code. I do not doubt the code has mistakes though, I do not know javascript. I wrote it solely by studying the parse.com documentation.
I'm not convinced that it makes sense to create these 2 independent messages and link them together, but I won't let that stand in the way of getting this working. This isn't tested, but I've refactored your code and think you should try to glean a few things from it.
// Set this up once, outside of your function, and use it everywhere
var GameMessage = Parse.Object.extend("GameMessages");
Parse.Cloud.define("findpartner", function(request, response) {
// Code defensively, make sure this function requires a user be logged in.
if (!request.user) {
console.log("non-user called findpartner");
return response.error("Unauthorized.");
}
// Get the user who called the function
var user = request.user;
// The end response is a number, apparently
var result = 0;
// The target player
var targetPlayer;
// The two messages that will be used if a match is found
var firstmsg = new GameMessage();
var secondmsg = new GameMessage();
// Create a Users query
var query = new Parse.Query(Parse.User);
query.equalTo("searching", 1);
query.notEqualTo("objectId", user.id);
query.limit(50);
// Remove public access to Find operations for Users in the Data Browser
// Use the master key to query, and use promise syntax.
query.find({ useMasterKey: true }).then(function(objects) {
result = objects.length;
// If no users were found searching, mark the user as searching and save
if (result == 0) {
user.set('searching', 1);
// Return the save promise
return user.save(null, { useMasterKey: true });
}
// Pick a random user out of the response
var indexNum = Math.floor((Math.random() * objects.length));
var targetPlayer = objects[indexNum];
// Set that user to no longer be searching and save
targetPlayer.set("searching", 0);
return targetPlayer.save(null, { useMasterKey: true }).then(function() {
firstmsg.set("sender", user.id);
firstmsg.set("receiver", targetPlayer.id);
firstmsg.set("sent", 0);
firstmsg.set("received", 0);
firstmsg.set("receiverName", targetPlayer.getUsername());
secondmsg.set("sender", targetPlayer.id);
secondmsg.set("receiver", user.id);
secondmsg.set("sent", 0);
secondmsg.set("received", 0);
secondmsg.set("receiverName", user.getUsername());
// Return the promise result of saving both messages
return Parse.Object.saveAll([firstmsg, secondmsg], { useMasterKey: true });
}).then(function(messages) {
// Set the pointers to reference each other
firstmsg.set("otherside", secondmsg.id);
secondmsg.set("otherside", firstmsg.id);
// Return the promise result of saving both messages, again
return Parse.Object.saveAll([firstmsg, secondmsg], { useMasterKey: true });
});
}).then(function() {
// All the stuff above has finished one way or the other, now we just need to
// send back the result. 0 if no match was made.
response.success(result);
}, function(error) {
response.error(error);
});
});
firstplayer.save();
secondplayer.save();
secondplayer.set("otherside",firstplayer.id); <<<
firstplayer.set("otherside",secondplayer.id); <<<
firstplayer.save();
secondplayer.save();
This is the part of code that you say not working. In parse doc you can see that .save() is a non blocking operation. Means the line firstplayer.save() goes immediately to next line(it wont block the thread for saving). So when you set id secondplayer.set("otherside",firstplayer.id) firstplayer.id is still undefined.
So if you want a synchronous logic, like save first_object then save second_object ,
you have to use call backs.
first_object.save({
success: function(saved_first_object) {
second_object.save({
success: function(saved_second_object) {
//process complete
},
failure: function(error){
}
})
},
failure: function(error) {
console.log(error);
}
})
You can also approach it using promises.
http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/
UPDATE: Based on question edit from OP trying promises
Try this
firstplayer.save()
.then(function(saved_firstPlayer){
firstplayer = saved_firstPlayer;
return secondplayer.save();
}).then(function(saved_secondplayer) {
secondplayer = saved_secondplayer;
return firstplayer.save({ otherside: secondplayer.id});
}).then(function(updated_firstplayer){
firstplayer = updated_firstplayer;
return secondplayer.save({ otherside: firstplayer.id});
}).then(function(updated_secondlayer){
secondplayer= update_secondplayer;
});