Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong format of user object saved in MongoDB after pasing thru beforeSave #589

Closed
mchun opened this issue Feb 23, 2016 · 7 comments · Fixed by #619
Closed

Wrong format of user object saved in MongoDB after pasing thru beforeSave #589

mchun opened this issue Feb 23, 2016 · 7 comments · Fixed by #619
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@mchun
Copy link

mchun commented Feb 23, 2016

This is my code for creating a new post:

 app.post('/',requireUser, function(req, res) {
    var parseUserMe = new Parse.User();
    parseUserMe.id = req.user.id;
    var post = new Post();
    post.set("Desc", req.body.Desc);
    post.set("createdBy", parseUserMe);
    post.save().then(function(post) {
      res.json({ id: post.id });
    }, function(error) {
      res.json({ error: error });
    });
  });

But inside MongoDB, the createdBy field is saved as

    "_p_createdBy": {
        "score": 59,
        "updatedAt": {
            "$date": "2016-02-23T04:06:29.402Z"
        },
        "objectId": "SPtdQePtm2",
        "__type": "Object",
        "className": "_User"
    },

instead of this:

"_p_createdBy": "_User$SPtdQePtm2",

which caused the Dashboard to crash and showed the following error on the server:

Uncaught internal server error. [TypeError: mongoObject[key].split is not a function] TypeError: mongoObject[key].split is not a function

@mchun mchun changed the title Wrong format of user object saved in MongoDB Wrong format of user object saved in MongoDB after pasing thru beforeSave Feb 23, 2016
@mchun
Copy link
Author

mchun commented Feb 23, 2016

I was frustrated as the problem only occurs when creating new post, not in comments or other objects. Now I can confirm the problem is related to the beforeSave trigger function. If I commented out the beforeSave function, the format is correct. Below is my beforeSave trigger function:

Parse.Cloud.beforeSave("Post", function(request,response) {
  var post=request.object;
  var postAuthor=post.get('createdBy');
  var photo1=request.object.get('photo1')
  Parse.Promise.as().then(function() {
    if (post.get('Category')==="所有 All")
      return Parse.Promise.error('請選擇分類');
    if (post.get('Category')==="書籍/雜誌  Books/Magazines")
      post.set('Category',"書籍/雜誌 Books/Magazines");
    if (post.get('Category')==="竉物用品 Pets")
      post.set('Category',"寵物用品 Pets");
    return Parse.Promise.as();
  }).then(function() {
    if (post.isNew()){
      if (post.get('postType')==="free"){
        postAuthor.increment('score');
        return postAuthor.save(null, {useMasterKey: true});
      }
    }else{
      if (post.get('postType')!==post.get('prevPostType')){
        if (post.get('postType')==='free')
          postAuthor.increment('score');
        else
          postAuthor.increment('score',-1);
        return postAuthor.save(null, {useMasterKey: true});
      }
    }
    return Parse.Promise.as();
  }).then(function(user) {
    post.set('prevPostType',post.get('postType'));
  }).then(function(){
    response.success();
  }, function(error) {
    response.error(error);
  });
});

@gfosco
Copy link
Contributor

gfosco commented Feb 23, 2016

cc @nlutsenko who has been looking in to the triggers recently... Looks like this pointer saved from cloud code isn't being transformed properly.

@gfosco gfosco added the type:bug Impaired feature or lacking behavior that is likely assumed label Feb 23, 2016
@nlutsenko
Copy link
Contributor

Yup, looks like it - looking into the best way to go about fixing it, but in the meantime - if you save the user before saving the post - it should properly resolve to full object save.

@nlutsenko
Copy link
Contributor

Got a test case that fails, that's a good start, it is attached to the fact that we don't collapse objects back into pointers for child objects that could be saved in the middle of running a beforeSave.

@mchun
Copy link
Author

mchun commented Feb 24, 2016

The problem also occurs when I used a fetched user i.e. req.user instead of parseUserMe.
And moreover, it seems the dirty function is not working inside trigger functions? The fields are always clean.

@nlutsenko
Copy link
Contributor

Yup, all of it is going to be fixed by the Pull Request attached to this issue.

@mchun
Copy link
Author

mchun commented Feb 25, 2016

Thanks so much, you guys are awesome!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants