云代码导致“试图通过指向新的未保存对象的指针来保存对象".保存指针时 [英] Cloud code causes "Tried to save an object with a pointer to a new, unsaved object" when saving pointers

查看:162
本文介绍了云代码导致“试图通过指向新的未保存对象的指针来保存对象".保存指针时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有用户和帖子,并且我试图实例化具有指向用户和帖子对象的指针的Activity类.这是我的代码:

In my app, I have users and posts, and I am trying to instantiate my Activity class which has pointers to user and post objects. Here is my code:

var Activity = Parse.Object.extend("Activity");
var User = Parse.Object.extend("_User");
var Post = Parse.Object.extend("Posts");

var activityHandler = function(request, response){
    //toUser is the objectId of the User not the object itself.
    var toUserId = request.toUser;
    var fromUserId = request.fromUser;
    var postId = request.post;
    var arg = request.argument;


    var toUser = new User();
    toUser.set("objectId",toUserId);
    var fromUser = new User();
    fromUser.set("objectId",fromUserId);
    var post = new Post();
    post.set("objectId",postId);

    var activity = new Activity();
    activity.set("activityFrom", fromUser);
    activity.set("activityTo", toUser);
    activity.set("argument", arg);
    activity.set("post", post);
    activity.save(null, {
        useMasterKey: true,

            success: function(o){
        console.log("activity logged successfully");
        response.success();
            },
            error: function(err){
        console.log(err);
            },
        }
    );
};

Parse.Cloud.define("createActivity", activityHandler);

该代码是不言自明的,但仅作总结,客户端发送适当的用户ID和帖子ID,云功能应实例化一个新的Activity对象,并带有指向用户和帖子的指针.但是,当我尝试保存时,我得到了:

The code is self-explanatory, but just to summarize, the client sends the appropriate user and post IDs, and the cloud function should instantiate a new Activity object, with pointers to the users and post. However, when I try to save, I'm getting:

{"code":141,"error":"Uncaught Tried to save an object with a pointer to a new, unsaved object."}

问题在于,发送给该函数的对象ID是完全有效的数据库中已经存在的对象. ACL也不是问题,因为我使用的是主密钥.

The problem is that, the object IDs sent to the function are completely valid, already-existing objects in the database. ACL is also not an issue as I'm on the master key.

我在做什么错了?

推荐答案

那是我的坏事.

我的代码有多个问题.

  1. 我使用的是object.set("objectId", id);语法,实际上应该是object.id = id.我不确定是否有必要,也许也可以在没有此更改的情况下使用,但是我到处都读到这是一个不好的做法.

  1. I was using object.set("objectId", id); syntax, which actually should be object.id = id. I'm not sure if it is necessary, maybe it would work without that change too, but I've read everywhere that it's a bad practice.

我没有正确获取请求参数.我使用的是request.param1而不是request.params.param1,并且参数变量未定义.我很长一段时间都没有检查过它是否未定义,因为从我的角度来看,该错误消息对于未定义参数并没有真正的帮助. (现在这很有意义,没有ID,它会自动尝试创建一个新对象)

I wasn't getting the request parameters correctly. I was using request.param1 instead of request.params.param1, and the parameter variable was undefined. I haven't checked if it was undefined or not for a long time, as the error message wasn't really helpful about parameter being undefined, from my perspective back then. (now it makes sense, no ID, and it automatically tries to create a new object)

我已经更正了它们,现在可以正确保存.

I've corrected them and it does save correctly now.

这篇关于云代码导致“试图通过指向新的未保存对象的指针来保存对象".保存指针时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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