beforeSave导致查询不返回任何内容

beforeSave导致查询不返回任何内容

本文介绍了beforeSave导致查询不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个云函数,它接受请求并创建对象,并将它们与一系列承诺相关联。如果没有beforeSave,这个函数可以正常工作,但是存在beforeSave是为了防止电子邮件类中电子邮件地址重复输入。



请求

  {
projectDescription:测试saveProject,
projectTitle:这只是一个测试,如果有真实的帖子这个将有一个实际的描述,
isEmailEnabled:true,
shareEmails:[
[email protected]
[email protected]
[email protected]
],
userId:1234
}

FUNCTION

  Parse.Cloud.define(saveProject,function(request) ,响应){

var emails = request.params.shareEmails;
var user = request.params.userId;
var projectDescription = request.params.projectDescription;
var projectTitle = request.params.projectTitle;
var emailStatus = request.params.isEmailEnabled;

var ProjectCla ss = Parse.Object.extend(Project);

var EmailsClass = Parse.Object.extend(Email);

var EmailsClassAssignment = Parse.Object.extend(EmailAssignment);

var project = new ProjectClass();

var projectO;

var emailQueryArray;

project.set(title,projectTitle);
project.set(createdBy,{
__ type:Pointer,
className:_ User,
objectId:user
});
project.set(description,projectDescription);
project.set(status,true);
project.set(emailShareEnabled,emailStatus);
project.save()。then(function(projectObject){

projectO = projectObject;
})。then(function(){
emails.forEach( function(emailAddress){
var email = new EmailsClass();

email.set(address,emailAddress);
email.save();
});
})。then(function(){

emails.forEach(function(emailQuery){
var queryEmail = new Parse.Query(Email);

queryEmail.equalTo(address,emailQuery);
queryEmail.find()。then(function(results){
emailObject = results;
console。 log(emailObject);
});
});
});
});

beforeSave Code

  Parse.Cloud.beforeSave(电子邮件,函数(请求,响应){

var query = new Parse.Query(Email);


//获取对象保存前的电子邮件密钥值(字符串)
query.equalTo(address,request.object.get(address));

//检查该电子邮件的对象是否已存在
query.first({

success:function(object){
if(object){
response.error(这封电子邮件已经存在);
} else {
response.success();
}
},
错误:函数(错误){
response.error(无法确定此电子邮件是否存在);
}
});
});

有没有办法通过保存电子邮件对象来解释回调中的beforeSave,以便我确保该对象是否需要查询?



我需要对象存在,以便我可以为每个对象获取Id并在Email assignments Class中创建一个指针。 / p>

很乐意进一步解释代码或函数的任何部分作为一个整体。

解决方案

save 就像 find 一样,是一种异步方法。要等到它们完成,并注意它们是否已经失败,你需要在循环中创建的所有promise上使用 Promise.when 。请参阅和类似问题。



您的代码应如下所示:

  var ProjectClass = Parse.Object.extend(Project); 
var EmailsClass = Parse.Object.extend(Email);
var EmailsClassAssignment = Parse.Object.extend(EmailAssignment);

Parse.Cloud.define(saveProject,function(request,response){
var emails = request.params.shareEmails;
var user = request.params.userId ;
var projectDescription = request.params.projectDescription;
var projectTitle = request.params.projectTitle;
var emailStatus = request.params.isEmailEnabled;

var project = new ProjectClass();
project.set(title,projectTitle);
project.set(createdBy,{
__type:Pointer,
className:_ User,
objectId:user
});
project.set(description,projectDescription);
project.set(status ,true);
project.set(emailShareEnabled,emailStatus);
project.save()。then(function(){
return Parse.Promise.when(emails.map( function(emailAddress){
// ^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^
var email = new EmailsClass();
email.set(地址,em ailAddress);
return email.save();
// ^^^^^^
}));
})。then(function(){
返回Parse.Promise.when(emails.map(function(emailQuery){
// ^^^^^^ ^^^^^ ^^^^^^^^^^^^^ ^^^
var queryEmail = new Parse.Query(Email);
queryEmail.equalTo(address,emailQuery);
返回queryEmail.find()。then(function(results){
// ^^^^^^
console.log(results);
});
}));
});
});


I have a cloud function that takes in a request and creates objects as well as associates them in a chain of promises. Without a beforeSave this function works fine but the beforeSave exists to prevent duplicate entries of email addresses in the Email Class.

REQUEST

{
   "projectDescription": "Testing saveProject",
   "projectTitle": "This is only a test, in the event of a real post          this will have an actual description",
   "isEmailEnabled": true,
   "shareEmails": [
     "[email protected]",
     "[email protected]",
     "[email protected]"
   ],
 "userId": "1234"
}

FUNCTION

Parse.Cloud.define("saveProject", function(request, response) {

    var emails = request.params.shareEmails;
    var user = request.params.userId;
    var projectDescription = request.params.projectDescription;
    var projectTitle = request.params.projectTitle;
    var emailStatus = request.params.isEmailEnabled;

    var ProjectClass = Parse.Object.extend("Project");

    var EmailsClass = Parse.Object.extend("Email");

    var EmailsClassAssignment = Parse.Object.extend("EmailAssignment");

    var project = new ProjectClass();

    var projectO;

    var emailQueryArray;

    project.set("title", projectTitle);
    project.set("createdBy", {
        "__type": "Pointer",
        "className": "_User",
        "objectId": user
    });
    project.set("description", projectDescription);
    project.set("status", true);
    project.set("emailShareEnabled", emailStatus);
    project.save().then(function(projectObject) {

        projectO = projectObject;
    }).then(function() {
        emails.forEach(function(emailAddress) {
            var email = new EmailsClass();

            email.set("address", emailAddress);
            email.save();
        });
    }).then(function() {

        emails.forEach(function(emailQuery) {
            var queryEmail = new Parse.Query("Email");

            queryEmail.equalTo("address", emailQuery);
            queryEmail.find().then(function(results) {
                emailObject = results;
                console.log(emailObject);
            });
        });
    });
});

beforeSave Code

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("This email already exisits");
    } else {
      response.success();
    }
  },
  error: function(error) {
    response.error("Could not determine if this email exists");
  }
});
});

Is there a way to account for the beforeSave in the callback from saving the email object so that I ensure the object is there to query against?

I need the objects to be there so that I can get the Id for each and create a pointer in the Email assignments Class.

Would be happy to further explain any part of the code or function as a whole.

解决方案

save is, just like find, an asynchronous method. To wait until it they have completed, and to notice whether they have failed, you need to use Promise.when on all the promises that have been created in the loop. See Parse JavaScript Promises within Loop not Completing and similar questions.

Your code should look like this:

var ProjectClass = Parse.Object.extend("Project");
var EmailsClass = Parse.Object.extend("Email");
var EmailsClassAssignment = Parse.Object.extend("EmailAssignment");

Parse.Cloud.define("saveProject", function(request, response) {
    var emails = request.params.shareEmails;
    var user = request.params.userId;
    var projectDescription = request.params.projectDescription;
    var projectTitle = request.params.projectTitle;
    var emailStatus = request.params.isEmailEnabled;

    var project = new ProjectClass();
    project.set("title", projectTitle);
    project.set("createdBy", {
        "__type": "Pointer",
        "className": "_User",
        "objectId": user
    });
    project.set("description", projectDescription);
    project.set("status", true);
    project.set("emailShareEnabled", emailStatus);
    project.save().then(function() {
        return Parse.Promise.when(emails.map(function(emailAddress) {
//      ^^^^^^ ^^^^^^^^^^^^^^^^^^        ^^^
            var email = new EmailsClass();
            email.set("address", emailAddress);
            return email.save();
//          ^^^^^^
        }));
    }).then(function() {
        return Parse.Promise.when(emails.map(function(emailQuery) {
//      ^^^^^^ ^^^^^^^^^^^^^^^^^^        ^^^
            var queryEmail = new Parse.Query("Email");
            queryEmail.equalTo("address", emailQuery);
            return queryEmail.find().then(function(results) {
//          ^^^^^^
                console.log(results);
            });
        }));
    });
});

这篇关于beforeSave导致查询不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:59