从Parse Class传递exisiting Object,否则创建一个新对象 [英] Pass exisiting Object from Parse Class otherwise create a new object

查看:71
本文介绍了从Parse Class传递exisiting Object,否则创建一个新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至目前,我有一个beforeSave函数,可以防止保存重复的电子邮件,如下所示。

As of right now I have a beforeSave function that prevents duplicate emails from being saved shown below.

Parse.Cloud.beforeSave("Email", function(request, response) {

    var query = new Parse.Query("Email");


// Gets the email key value (string) before object is saved
    query.equalTo("address", request.object.get("address"));

// Checks to see if an object for that email already exists
    query.first({

        success: function(object) {
          if (object) {
             response.error("Email already Exists");
          } else {
             response.success();
        }
      },
      error: function(error) {
      response.error("Could not determine if this email exists");
       }
    });
});

创建电子邮件的功能,但如果有人在项目中添加了我们之前保存的电子邮件地址,至少在我的理解中,打破函数并最终破坏这个函数存在的承诺链。

Function that creates emails but if someone adds an email address to their project that we have saved before this will break the function and ultimately the chain of promises that this function exists in, at least in my understanding.

function createEmails(emails) {
    var EmailsClass = Parse.Object.extend("Email");
    var toSave = _.map(emails, function(emailAddress) {
        var email = new EmailsClass();
        email.set("address", emailAddress);
        return email;
    });

    return Parse.Object.saveAll(toSave);
}


推荐答案

希望有人有更好的答案比这更好,但似乎避免在之前保存之前保留的唯一方法是回复一个错误,这会使后续处理失败。

Hopefully somebody has a better answer than this, but it seems that the only way to avoid saving on beforeSave is to respond with an error, and that flummoxes subsequent processing.

因此,如果从应用程序的角度来看,尝试创建副本不是错误,只是一个不允许的条件,那么我认为你必须在 beforeSave ,通过过滤重复项的输入。

So, if attempting to create a duplicate is not an error from an application point of view, just a condition to be disallowed, then I think you must catch that before beforeSave, by filtering the input for duplicates.

在批量创建时这并不是那么漂亮,但它会是这样的:

That's not so pretty when creating in bulk, but it would go something like this:

// answer a promise for an existing email object with address==email or a promise to save a new one
function fetchOrCreateEmail(emailAddress) {
    var EmailsClass = Parse.Object.extend("Email");
    var query = new Parse.Query(EmailsClass);
    query.equalTo("address", emailAddress);
    return query.first().then(function(object) {
        if (!object) {
            object = new EmailsClass();
            object("address", emailAddress);
            return object.save();
        } else {
            return Parse.Promise.as(object);
        }
    });
}

// create Email objects with a given array of addresses, creating
// objects only for unique addresses
function createEmails(emails) {
    var toSave = _.map(emails, fetchOrCreateEmail);
    return Parse.Promise.when(toSave);
}

使用此 fetchOrCreateEmail 函数,您可以如图所示批量创建,或者像这样创建一个:

With this fetchOrCreateEmail function, you can create in batch as shown, or create a single like this:

fetchOrCreateEmail(emailAddress);

如果您将这些用于所有电子邮件对象的创建,您将不再需要(或想要)该类的 beforeSave 挂钩。我想知道是否有比这更好的解决方案,但我认为这是目前最好的解决方案。

If you use these for all creation of Email objects, you'll no longer need (or want) the beforeSave hook for that class. I'd like to find out if there's a better solution than this one, but I think that's the best that can be done at present.

这篇关于从Parse Class传递exisiting Object,否则创建一个新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆