parse-community / Parse-SDK-JS

The JavaScript SDK for Parse Platform
https://parseplatform.org
Apache License 2.0
1.32k stars 597 forks source link

Objects are not properly updated #59

Closed dennito closed 8 years ago

dennito commented 9 years ago

Hey guys, I've a weird problem. I retrieve objects from my database (Parse.com) and need to update them later on. But if I update them, they're sometimes re-created (new objects are created with the new values). But only sometimes. Below you find the code. Am I something missing?

[...] var codes = ... // load objects

var response = JSON.parse(response.text); if (response[0].status == "sent" || response[0].status == "queued"){ for (var i=0; i<codes.length; i++){ codes[i].set("sent", true); } }

return Parse.Object.saveAll(codes);

andrewimm commented 9 years ago

Is this running in cloud code, or a client? We need to know more about how you're loading the objects as well. Objects will be re-created on the db if the objects you save don't have objectIds set on them, which can happen if you're improperly inflating the objects.

In general, we prioritize issues that have a reproducible test case. If you can write up a small block of code that causes this to happen, we can debug it; otherwise, it could be due to any variety of reasons that we don't have insight into.

parse-github-bot commented 9 years ago

Thank you for your feedback. We prioritize issues that have clear and concise repro steps. Please see our Bug Reporting Guidelines about what information should be added to this issue.

Please try the latest SDK. Our release notes have details about what issues were fixed in each release.

In addition, you might find the following resources helpful:

dennito commented 9 years ago

Yes, it's running on Cloud Code. See below the complete code.

// These two lines are required to initialize Express in Cloud Code.
 express = require('express');
 app = express();

// Global app configuration section
app.set('views', 'cloud/views');  // Specify the folder to find templates
app.set('view engine', 'ejs');    // Set the template engine
app.use(express.bodyParser());    // Middleware for reading request body

app.post('/mandrill-webhook', function(req, res){
var events = JSON.parse(req.body.mandrill_events);

var query = new Parse.Query("Vouchercode");
query.notEqualTo("sent", true);
query.equalTo("email", null);
query.limit(events.length);
query.find().then(function(codes){

    if (codes.length == 0){
        return Parse.Promise.as("No codes left");
    }

    console.log(events.length);
    for (var i=0; i<events.length; i++){

        var msg = events[i].msg;
        var email = msg.email;
        var template = msg.template;
        console.log(email);

        codes[i].set("email", email);

    }

    return Parse.Object.saveAll(codes);

}).then(function(codes){
    console.log("Code vergeben!");
    res.send(200);

}, function(error){
    console.log(error.message);
    res.send(500);
});

});

Parse.Cloud.job("sendMail", function(req, status){
    Parse.Cloud.useMasterKey();
    var emails = [];
    var codes = null;
    var query = new Parse.Query("Vouchercode");
    query.notEqualTo("sent", true);
    query.notEqualTo("email", null);
    query.limit(50);
    query.find().then(function(codesTmp){
     codes = codesTmp;

    console.log(codes.length);
    if (codes.length == 0){
        return Parse.Promise.error("No codes left");
    }

    var mergeVars = [];
    var subscribers = [];

    for (var i=0; i<codes.length; i++){
        var mv = {rcpt: codes[i].get("email"), vars: [{name: "code", content: codes[i].get("code")}]};
        var s = { email: codes[i].get("email"), name: null };
        mergeVars.push(mv);
        subscribers.push(s);
    }

    var message = {
        to: subscribers,
        //subject: subject,
        //merge_language: "handlebars",
        merge_vars: mergeVars
    };

    //console.log(subscribers);
    //console.log(JSON.stringify(codes));

    return Parse.Cloud.httpRequest({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        },
        url: 'https://mandrillapp.com/api/1.0/messages/send-template.json',
        body: {
            template_name: "some-template",
            template_content: null,
            key: "some-key",
            message: message,
            async: false
        }});

}).then(function(response){
    var response = JSON.parse(response.text);
    console.log(response);
    if (response[0].status == "sent" || response[0].status == "queued"){
        console.log("YEP - IS SENT");
        for (var i=0; i<codes.length; i++){
            codes[i].set("sent", true);
        }
    }

    return Parse.Object.destroyAll(codes);

}, function(response){
    //var response = JSON.parse(response.text);
    //console.log(response);
    //status.error(response);
    return Parse.Promise.error("No codes left");
}).then(function(){
    status.success("success!");
}, function(error){
    //console.log(error.message);

    //console.log(JSON.stringify(error));
    status.error(error);
});

});

// Attach the Express app to Cloud Code.
app.listen();
dennito commented 9 years ago

The issue must be in the job. So you can ignore the other stuff.

parse-github-bot commented 9 years ago

This issue has not been updated for 7 days. If you have additional information to help pinpoint this issue as an SDK bug, please comment on this issue. We will close this issue in 7 days if no additional information is provided. Thank you for your feedback.

parse-github-bot commented 8 years ago

We are closing this issue due to another 7 days of inactivity. If you have additional information to help pinpoint this issue as an SDK bug, please reopen it with the additional information.Thank you for your feedback.

parse-github-bot commented 8 years ago

This issue has not been updated for 7 days. If you have additional information to help pinpoint this issue as an SDK bug, please comment on this issue. We will close this issue in 7 days if no additional information is provided. Thank you for your feedback.

parse-github-bot commented 8 years ago

We are closing this issue due to another 7 days of inactivity. If you have additional information to help pinpoint this issue as an SDK bug, please reopen it with the additional information.Thank you for your feedback.

parse-github-bot commented 8 years ago

This issue has not been updated for 7 days. If you have additional information to help pinpoint this issue as an SDK bug, please comment on this issue. We will close this issue in 7 days if no additional information is provided. Thank you for your feedback.

parse-github-bot commented 8 years ago

We are closing this issue due to another 7 days of inactivity. If you have additional information to help pinpoint this issue as an SDK bug, please reopen it with the additional information.Thank you for your feedback.

parse-github-bot commented 8 years ago

This issue has not been updated for 7 days. If you have additional information to help pinpoint this issue as an SDK bug, please comment on this issue. We will close this issue in 7 days if no additional information is provided. Thank you for your feedback.

parse-github-bot commented 8 years ago

We are closing this issue due to another 7 days of inactivity. If you have additional information to help pinpoint this issue as an SDK bug, please reopen it with the additional information.Thank you for your feedback.