I'm trying to get the results of the following sql command in Parse.com using cloud code.
Select shop, count(1) from Membership group by shop;
Is there a way to do so? or I can only get the number of members from selecting each shop?
var query = new Parse.Query("Membership");
query.equalTo("shop",shop_id);
var promise = query.find().then(function(results){
var number_of_membership_of_one_shop = results.leng
return results;
});
Parse unfortunately doesn't support group by.
You could first select all shops and then use a count query for each shop.
var query = new Parse.Query("Membership");
query.equalTo("shop",shop_id);
var promise = query.count().then(function(count){
var number_of_membership_of_one_shop = count;
return number_of_membership_of_one_shop;
});
If this is performing too many requests, you could select all the memberships and then count them on the client, but this will have a limit of 1000 so you may need to adopt some other techniques:
var query = new Parse.Query("Membership");
query.select("shop_id");
query.limit(1000);
var storeCounts = [];
queryObject.find({
success: function (results) {
for (var i = 0; i < results.length; i++) {
var shopId = results[i].get('shop_id');
if (!storeCounts[shopId]) {
storeCounts[shopId] = 0;
}
storeCounts[shopId]++;
}
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
Related
Hi I'm currently working on a Twitter bot with the Twitter API and Node.JS. I want the bot to return all of my followers and some of their features in an javascript object. Something like :
{['id', 'screen_name', 'name', 'screen_name', 'followers_count',
'friends_count']}
RN my code is :
var Twitter = new TwitterPackage(config);
var options =
{
screen_name: 'mowsolicious',
};
Twitter.get('followers/ids', options, function (err, data) { // returns a list of ids
var nbFollowers = data.ids.length
var id = []
console.log(nbFollowers) // how many followers I have
for (i=0 ; i <= nbFollowers ; i++) {
ids = data.ids
var id = ids[i]
Twitter.get('users/show/' + id, function(err, data) {
console.log(id + " - " + data.name + " - " + data.screen_name + " - " + data.followers_count + " - " + data.friends_count)
})
}
})
I'm pretty sure something is terribly wrong with my method (more precisely when I put the Twitter.get thing in the loop) and it returns a bunch of undefined in the console.
I tried to work with the API doc but I'm experiencing some troubles understanding it. If someone could help that would be great.
Thank you
Most likely, you get undefined because the user is not found :
[ { code: 50, message: 'User not found.' } ]
Checking err variable would take care of that. But looking at GET followers/id documentation, you should use GET users/lookup to efficiently request mutliple user objects (up to 100 user per request with user id delimited by comma)
Also, I assume you'd like a unique callback to be called when all requests are completed, using Promises will take care of that :
var res_array = [];
function getUserInfo(id_list) {
return Twitter.get('users/lookup', {
"user_id": id_list
}).then(function(data) {
res_array.push(data);
})
.catch(function(error) {
throw error;
})
}
Twitter.get('followers/ids', function(err, data) {
if (err) {
console.log(err);
return;
}
console.log("total followers : " + data.ids.length);
var requestNum = Math.floor(data.ids.length / 100);
var remainder = data.ids.length % 100;
var promises_arr = [];
for (var i = 0; i < requestNum; i++) {
promises_arr.push(getUserInfo(data.ids.slice(i * 100, i * 100 + 100).join(",")));
}
if (remainder != 0) {
promises_arr.push(getUserInfo(data.ids.slice(requestNum * 100, requestNum * 100 + 100).join(",")));
}
Promise.all(promises_arr)
.then(function() {
for (var i in res_array) {
for (var j in res_array[i]) {
var user = res_array[i][j];
console.log(user.id + " - " +
user.name + " - " +
user.screen_name + " - " +
user.followers_count + " - " +
user.friends_count)
}
}
})
.catch(console.error);
})
List of followers can be retrieved with superface sdk , try it based on the example below
npm install #superfaceai/one-sdk
npx #superfaceai/cli install social-media/followers
const { SuperfaceClient } = require('#superfaceai/one-sdk');
const sdk = new SuperfaceClient();
async function run() {
// Load the installed profile
const profile = await sdk.getProfile('social-media/followers');
// Use the profile
const result = await profile
.getUseCase('GetFollowers')
.perform({
profileId: '429238130'
});
return result.unwrap();
}
run();
This works pretty fine
import fetch from 'node-fetch'
async function getFollowers(username) {
const response = await fetch(`https://api.twitter.com/1.1/followers/list.json?screen_name=${username}`, {
headers: {
Authorization: `Bearer ${<yourbearertoken>}`
}
});
// Parse the response as JSON
const data = await response.json();
return data.users;
}
const followers = await getFollowers("<username>");
So we have large data in JSON format.
We want to save it to a class (table) in our Parse app.
I wrote a JS script which can read the file and go through the JSON data.
But when is do the saving it all gets messed up. Its loops in the first one for ever. I understand that there is something called promise bt I don't understand how to use it? Can anyone help. My code is given below.
function processJson(result) {
object = JSON.parse(result);
verbose.textContent = "Read " + object.results.length + " objects";
var count = object.results.length;
var countAc = 0;
logger("To save: " + count);
i = 0;
while (i < count) {
if (object.results[i].areaType == 'ac') {
save(i).then(function (object) {
i = i + 1;
logger("Success: " + object.id);
});
} else {
logger("ac not found");
i = i + 1;
}
}
}
function save(i) {
logger("ac found");
var constituency = new Constituency();
constituency.set("points", object.results[i].points);
constituency.set("areaType", object.results[i].areaType);
constituency.set("name", object.results[i].name);
constituency.set("state", object.results[i].state);
constituency.set("index", object.results[i].index);
constituency.set("pc", object.results[i].pc);
constituency.set("center", object.results[i].center);
constituency.set("oldObjectId", object.results[i].objectId);
return constituency.save();
/*constituency.save().then(function(obj) {
// the object was saved successfully.
i = i + 1;
logger("Success: " + obj.id);
}, function(error) {
// the save failed.
logger(error.message);
i = i + 1;
});*/
}
I would do something like that:
function processJson(result) {
var object = JSON.parse(result);
for (var i = 0; i < object.results.legnth; i++){
var parseObject = createParseObjectFromJSONObject(object.results[i]);
parseObject.save(null).then(function(object){
console.log("object saved: " + object.id);
},function(error){
console.log("error: " + error);
});
}
}
function createParseObjectFromJSONObject(jsonObject){
var constituency = new Constituency();
constituency.set("points", jsonObject.points);
constituency.set("areaType", jsonObject.areaType);
constituency.set("name", jsonObject.name);
constituency.set("state", jsonObject.state);
constituency.set("index", jsonObject.index);
constituency.set("pc", jsonObject.pc);
constituency.set("center", jsonObject.center);
constituency.set("oldObjectId", jsonObject.objectId);
return constituency;
}
You can do it even better..
You can first push all the parse objects into array and then call saveAll to save all the parse objects in one request. This solution is good for < 1000 records .. if you have more than 1000 then you can do paging (first 1000 and saveAll, other 1000 and saveAll ....)
In this version your code will look like this:
function processJson(result) {
var object = JSON.parse(result);
var allObjects = [];
for (var i = 0; i < object.results.legnth; i++){
var parseObject = createParseObjectFromJSONObject(object.results[i]);
allObjects.push(parseObject);
}
// outside the loop we are ready to save all the objects in
// allObjects array in one service call!
if (allObjects.length > 0){
Parse.Object.saveAll(allObjects).then(function(){
console.log("all objects were saved!");
// all object ids are now available under the allObjects array..
},function(error){
console.log("error: " + error);
});
}
}
function createParseObjectFromJSONObject(jsonObject){
var constituency = new Constituency();
constituency.set("points", jsonObject.points);
constituency.set("areaType", jsonObject.areaType);
constituency.set("name", jsonObject.name);
constituency.set("state", jsonObject.state);
constituency.set("index", jsonObject.index);
constituency.set("pc", jsonObject.pc);
constituency.set("center", jsonObject.center);
constituency.set("oldObjectId", jsonObject.objectId);
return constituency;
}
Good Luck :)
I have a Parse Cloud Code function that will act as my background job. Right now I am in my debugging stage. The function does not throw an error and it is not doing what it is supposed to do. The function entitled "backgroundJob" is supposed to go through all the "Group" objects that have been created. Each "Group" object has an Array of "Event" objects, and all you have to know about the "Event" object is it has a date property called "date".
The function should go through all the events for each group object and if the event's date is the same as the current time the function should send a notification to all "Users" who are apart of that group and then delete that event. Right now the function is not deleting the event if it is the same time as the current time and is not notifying members of the group.
My function is below.
Parse.Cloud.define("backgroundJob", function(request, response) {
Parse.Cloud.useMasterKey();
var moments = require("cloud/moment.js");
moments().format();
var _ = require('cloud/underscore.js');
// Get the actual time, for use in non testing
// var now = moments();
// For testing edit time in ()
var now = moments("2015-23-11 19:30", "YYYY-MM-DD HH:mm");
var out = now;
console.log(out);
var groupObject = Parse.Object.extend("Group");
var query = new Parse.Query(groupObject);
var eventObject = Parse.Object.extend("Event");
query.find().then(function(groups) {
var promise = Parse.Promise.as();
var groupArray = groups;
for (var i = 0; i < groupArray.length; i++) {
promise = promise.then(function() {
var count = 0;
var eventArray = groupArray[i].get("Events");
for (count = 0; count < eventArray.length; count++) {
if (now == eventArray[count].get('date')) {
var curEvent = eventArray[count];
eventArray[count].destory();
var relationc = result.get("created");
var createdq = relationc.query();
var relationj = result.get("created");
var joinedq = relationj.query();
var partOnee = curEvent.get("name");
var outString = partOnee.concat(" is now");
Parse.Push.send({
where : createdq,
data : {
alert : outString
}
}).then(function() {
response.success();
}, function(error) {
response.error(error);
});
Parse.Push.send({
where : joinedq,
data : {
alert : outString
}
}).then(function() {
response.success();
}, function(error) {
response.error(error);
});
var e = eventArray[count];
var destroyp = Parse.Promise.as();
destroyp = promise.then(function() {
return e.destroy();
}, function(error) {
response.error(error);
});
}
}
});
}
}).then(function() {
response.success()
}, function(error) {
response.error(error);
});
});
Thank you.
For reference in the code there are two separate relations to users a "created" and a "joined", this is why in the code I have a created query and a reaction query.
You have a typo in your code. Maybe this is not the cause of the issue, but anyways, check it.
eventArray[count].destory();
Seems silly, but, who knows...
I'm trying to add unique objects to my parse database, that is, the name property must be different for each object. I try to query for all objects with a given name like so
var query = new Parse.Query(Food);
query.exists("name", name);
query.count({
success: function(number) {....}
However, query.count is always the total number of objects (90) stored on the database, even though there should be 0 or 1 objects with a given name stored.
EDIT:
Following one of the answers, I modified the code to this. However, I still see duplicates in the database.
var query = new Parse.Query(Food);
query.equalTo("name", name);
query.first({
success: function(results) {...}
Below is the entire (edited) function
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var foodListCorrected = new Array();
var Food = Parse.Object.extend("Food");
// Wrap your logic in a function
function process_food(i) {
// Are we done?
if (i == foodList.length) {
//console.log("count is " + foodListCorrected.length);
Parse.Object.saveAll(foodListCorrected, {
success: function(foodListCorrected) {},
error: function(foodListCorrected) {}
});
return;
}
var name = foodList[i];
//console.log("before name is " + name);
var query = new Parse.Query(Food);
query.equalTo("name", name);
query.first({
success: function(results) {
if(!results){
console.log("new");
var food = new Food();
food.set("name", name);
foodListCorrected.push(food);
// console.log(foodListCorrected.length);
} else {
//don't create new food
console.log("exists");
}
process_food(i+1)
},
error: function(error) {
console.log("error");
}
});
}
// Go! Call the function with the first food.
process_food(0);
});
EDIT:
I've tried an alternate approach to this problem using promises, but the line
return Parse.Object.saveAll(foodListCorrected);
does not seem to be executing. I've verified that foodListCorrected is a non-empty array. Below is the entire code for the function.
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var foodListCorrected = new Array();
var Food = Parse.Object.extend("Food");
var query = new Parse.Query(Food);
query.find().then(function(foods) {
for (i = 0; i < foodList.length; i++) {
var j;
for (j = 0; j < foods.length; j++){
if (foodList[i] == foods[j])
break;
}
if (j==foods.length)
foodListCorrected.push(foodList[i]);
}
console.log(foodListCorrected.length);
return Parse.Object.saveAll(foodListCorrected);
}).then(function() {
// Everything is done!
})
});
Try this:
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var saveThese = [];
var FoodClass = Parse.Object.extend("Food");
for(var i = 0; i < foodList.length; ++i){
var food = new FoodClass();
food.set("name", foodList[i]);
saveThese.push(food);
}
Parse.Object.saveAll(saveThese, {
success: function(list){
response.success();
},
error: function(error){
response.error("Failure on saving food");
}
});
});
//If a food already exists in the database, then don't save it
Parse.Cloud.beforeSave("Food", function(request, response){
var query = new Parse.Query("Food");
query.equalTo("name", request.object.get("name"));
query.count({
success: function(count){
if(count > 0)
response.error("Food already exists");
else
response.success();
},
error: function(error){
response.error(error);
}
});
});
The "exists" condition takes in one argument, the name of a key and sees if it exists for a row. So it will return every row where your "name" column has a value, any value. What you're looking for is something like this:
var query = new Parse.Query(Food);
query.equalTo("name", name);
query.first(...)
I write some simple app for windows 8 Metro UI with javascript. Because natural method from microsoft to use Sqlite with Javascript in Metro UI. I use 'doo' wrapper:
dooWrapper SQLite (github)
I create a method :
function addSomething(name) {
var dbPath = Windows.Storage.ApplicationData.current.localFolder.path + '\\a_db.sqlite';
SQLite3JS.openAsync(dbPath).then(function (db) {
return db.runAsync("INSERT INTO STH (nazwa) VALUES (:name)", { name: name }).
done(function () {
console.log('Add sth : ' + name);
db.close();
}, function (error) {
if (db) {
db.close();
}
console.log('ERROR Adding sth' + error.message);
})
});
}
I get error 'database is locked' I read about this error in documentation. But I have one question is other solution to add more rows without create 'insert' function with collections argument something like that : insert (array) ? I just want to use that function n-times without this error. That's possible?
Yes,it possible...i also got this error before....For that you just need to establish the database connection once...i have used this in my app and its working fine.
If there is no need of closing your db then then open database once like..
Add this code to default.js file
var myDatabase; //Global Variable
var dbPath = Windows.Storage.ApplicationData.current.localFolder.path + '\\db.sqlite';
//Create Table
SQLite3JS.openAsync(dbPath).then(function(db) {
myDatabase=db;
return db.runAsync('CREATE TABLE Item (name TEXT, price REAL, id INT PRIMARY KEY)');
});
Then you just need to use below code
// For Insert
return myDatabase.runAsync('INSERT INTO Item (name, price, id) VALUES ("'+ array[i].name+'", "48484", 1);
For array
var dbPromises = [];
var testArray = [];
//only for test purpose
//You can pass your array here directly
for (var a = 0; a < 300; a++) {
var obj = {
name: "Mango"+a,
price: 100+a,
id: a
};
testArray.push(obj);
}
for (var i = 0; i < testArray.length; i++) {
var query = 'INSERT OR REPLACE INTO Item (name, price, id) VALUES ("' + testArray[i].name + '",' + testArray[i].price + ',' + testArray[i].id + ')';
dbPromises.push(globalDatabase.allAsync(query));
}
WinJS.Promise.join(dbPromises).then(function () {
debugger;
}, function(err) {
debugger;
});
Above code is used only for less array size...bcz its taking too much time for insertion...
For fasst execution you should replace just below code
for (var i = 0; i < testArray.length; i++) {
var val = '("' + testArray[i].name + '",' + testArray[i].price + ',' + testArray[i].id + '),';
query = query + val;
if ((i + 1) % 300 == 0 || (i + 1) == testArray.length) {
query = query.replace(/,$/, "");
dbPromises.push(globalDatabase.allAsync(query));
query = 'INSERT OR REPLACE INTO Item (name, price, id) VALUES ';
}
}