Cloud Code function not saving data

僤鯓⒐⒋嵵緔 提交于 2019-12-11 15:05:35

问题


Before putting this on the cloud code I tested it in Angular with success producing the correct console.log responses throughout the program. Since this function manipulates data in the user table it must use the master key and be in cloud code. With this code in the cloud it saves the column 'duty' to the user table but with no data (there is data to be saved, this I am sure of). Moreover, I'm not even sure that the code runs past the first Parse Query as the console.log returns nothing in the Parse Logs. Where am I going wrong?

'use strict';
var express = require('express');
var app = express();
app.use(express.bodyParser());
var _ = require('underscore');
var server = require('http').createServer(app);

Parse.Cloud.define("updateMerchant", function(request, response) {
Parse.Cloud.useMasterKey();
var user = Parse.Object.extend("User")
var merchantQuery = new Parse.Query(Parse.User);
var Offers = Parse.Object.extend("Offer");
var offerQuery = new Parse.Query(Offers);
var Matches = Parse.Object.extend("Matched");
var matchQuery = new Parse.Query(Matches);

var merchantDuty = [];
var merchants = request.params.data;//I confirmed the validity of this a key value pair where the value is an array of objects.
var merchantIds = _.map(merchants, function(n){return n.id});
console.log(merchantIds)

offerQuery.containedIn("user", merchants);
offerQuery.limit(1000); 
offerQuery.find({//CODE STOPS RUNNING?!?
     success: function (offers) {
          var offerIds = _.map(offers, function (n) {
                         return n.id});

console.log(offers)//this is telling as it does not appear in the Parse log!

var offersBeta = _.map(offers, function (n) {
return _.extend(_.find(n), {id: n.id})});

     matchQuery.containedIn("offer", offers);
     matchQuery.limit(1000);
     matchQuery.find({
          success: function (matches) {
                   var merchantArray = _.map(_.flatten(matches), function (n) {return _.find(n)});

var offers3 = _.map(offersBeta, function (n) {return _.extend(n, {
Matched: _.filter(merchantArray, function (a) {return a.offer.id == n.id})})})

var duty = function (TotalBill, id) {
var promise = new Parse.Promise();
merchantQuery.get(id, {
     success: function (merchantBill) {
          merchantBill.set("duty", TotalBill);
          merchantBill.save().then(function(obj){ console.log(obj); }, function(error){console.log(error)})}})}

merchantDuty.push(duty(_.map(offer9, function(n){return n.TotalBill}), _.map(offer9, function(n){return n.id})));
},
error: function(){console.log(error);
}
})
}
})
//Code begins running again!

return Parse.Promise.when(merchantDuty).then(function() {

response.success("Success");
},
function(error) {response.error("Something is still wrong");
console.log(error);})
})

To be more clear, nothing between offerQuery.find and return Parse.Promise is run.


回答1:


You need to pass pointers in offerQuery.containedIn("user", merchants);. See this.

Try this:

var _ = require('underscore');


Parse.Cloud.define("updateMerchant", function(request, response) {
  Parse.Cloud.useMasterKey();

  var merchantDuty = [];
  var merchants = request.params.data;//I confirmed the validity of this a key value pair where the value is an array of objects.

  // var merchantIds = _.map(merchants, function(n) {return n.id;});
  // console.log(merchantIds);

  // Since I don't have the merchants request parameter, I'll fake it with some fake users
  var fakeMerchants = [{"username":"Batman","objectId":"f7zZkPx7kT","createdAt":"2015-04-07T19:41:25.014Z","updatedAt":"2015-04-07T19:41:25.014Z","__type":"Object","className":"_User"},{"username":"Robin","objectId":"wgG4EfaFN1","createdAt":"2015-04-07T19:41:35.024Z","updatedAt":"2015-04-07T19:41:35.024Z","__type":"Object","className":"_User"}];
  // We can get some users like this:
  // var fakeMerchantsQuery = new Parse.Query(Parse.User);
  // fakeMerchantsQuery.find().then(function(users) {
  //   console.log(users);
  // });

  // Since the 'user' column in Offer Class is a pointer, we need to pass merchant pointers.
  // Otherwise we get the error "pointer field user needs a pointer value"
  // See https://www.parse.com/questions/using-containedin-with-array-of-pointers
  var fakeMerchantsPointers = _.map(fakeMerchants, function(merchant) { // TODO change to real merchants
    var pointer = new Parse.User();
    pointer.id = merchant.objectId;
    return pointer;
  });

  console.log(fakeMerchantsPointers);

  var offerQuery = new Parse.Query(Parse.Object.extend("Offer"));
  offerQuery.containedIn("user", fakeMerchantsPointers); // TODO change to real merchants
  offerQuery.limit(1000);
  offerQuery.find().then(function(offers) {

    console.log("inside offer query");
    console.log(offers);

    // Here I assume that the column 'offer' is a Pointer
    var matchQuery = new Parse.Query(Parse.Object.extend("Matched"));
    matchQuery.containedIn("offer", offers);
    matchQuery.limit(1000);
    return matchQuery.find();

  }).then(function(matches){

    console.log("inside matches query");
    console.log(matches);

    // Add the duty stuff here...        

    // We must call success or error
    response.success("Success");

  });

});

Let me know if it worked.

Please note that you shouldn't mix Cloud Code with ExpressJS code. The Cloud Code should be in main.js, and the ExpressJS code in app.js. Then, in Cloud Code main.js call require('cloud/app.js'); if you want the request pass through ExpressJS.




回答2:


The line return Parse.Promise.when(merchantDuty) is executing before there are any promises in the merchantDuty array (initialized as empty). So the whole function is terminating before your query find success function.

I think if you create and add query promises to the merchantDuty array you will fix your bug.

I also suggest you to use promise callbacks for the query methods. Like:

query.find().then(function(){
  //success
}, function(error){
  //error
});

You can then chain them by returning another promise and make the code better structured.



来源:https://stackoverflow.com/questions/29499180/cloud-code-function-not-saving-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!