【问题标题】:Cloud Code function not saving data云代码功能不保存数据
【发布时间】:2015-04-08 05:28:10
【问题描述】:

在将它放到云代码上之前,我在 Angular 中对其进行了测试,并成功地在整个程序中生成了正确的 console.log 响应。由于此函数操作用户表中的数据,因此它必须使用主密钥并位于云代码中。使用云中的此代码,它将“职责”列保存到用户表中,但没有数据(我确信有数据要保存)。此外,我什至不确定代码是否通过了第一个 Parse Query,因为 console.log 在 Parse Logs 中没有返回任何内容。我哪里错了?

'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);})
})

更清楚地说,offerQuery.find 和 return Parse.Promise 之间没有运行。

【问题讨论】:

  • merchantIds 打印在日志中,对吧?
  • @kodingralph 是对的,但在 offerQuery.find 和返回语句之间没有任何内容打印在日志中。

标签: javascript angularjs parse-platform parse-cloud-code


【解决方案1】:

您需要在offerQuery.containedIn("user", merchants); 中传递指针。见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");

  });

});

让我知道它是否有效。

请注意,您不应将 Cloud Code 与 ExpressJS 代码混用。 Cloud 代码应该在main.js,ExpressJS 代码应该在app.js。然后,如果您希望请求通过 ExpressJS,请在 Cloud Code main.js 中调用 require('cloud/app.js');

【讨论】:

    【解决方案2】:

    return Parse.Promise.when(merchantDuty) 行在 MercerDuty 数组中没有任何承诺之前执行(初始化为空)。 所以整个函数在你的查询找到成功函数之前终止。

    我认为,如果您创建查询承诺并将其添加到 MercerDuty 数组中,您将修复您的错误。

    我还建议您对查询方法使用 Promise 回调。喜欢:

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

    然后,您可以通过返回另一个 Promise 将它们链接起来,并使代码结构更好。

    【讨论】:

    • 这不太奏效,但它离我们更近了一步。我需要像选择的答案一样链接所有这些。
    猜你喜欢
    • 2016-03-23
    • 2021-10-12
    • 2019-08-26
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多