如何在解析中将objectId保存到其他class?

How to save objectId to other class in parse?

我们有两个 classes "User""Schools"。学校 class 包含 "objectId" 列。

我想将 "objectId" 列保存到 "currentSchool" table 列中的 "User",但出现以下错误:

{"code":111,"error":"invalid type for key currentSchool, expected *Schools, but got string"}

这是代码片段:

Parse.User.signUp(username, password,
{
    firstName: $scope.vm.form.firstName,
    lastName: $scope.vm.form.lastName,
    fullName: $scope.vm.form.firstName + " " + $scope.vm.form.lastName,
    email: email,
    onboardApplicantId: $scope.applicantId,
    currentSchool: schooolId,
    trainingStatus: "videos",
    videoStatus: 0,
    ACL: new Parse.ACL()
}, {
    success: function (user) {
        $("#signupForm").trigger('reset');
        console.log(' success ');
        $('.signupMsg').text("Account created successfully. Please verify your email address.");
        $('.submit-button').attr("disabled", false);
    },
    error: function (user, error) {
        $('.signupMsg').text(error.message);
        $('.submit-button').attr("disabled", false);
    }
});

@Mazel Tov 其实是对的:

如果您查看 DB 的 SCHEMA(如果您是 运行 开源版本),否则如果您查看用户 [=32] 中的 "School" 选项卡名称=],你会看到它的类型是School.

这意味着您需要向 Parse 发送一个代表学校的 Parse.Object 而不是它的 id :)(因为 id 是一个字符串,这就是您的错误来源)

所以要修复它:

选项 1:您获取了您拥有的学校列表,您可以再次将它们传递给 Parse。

选项 2:您可以通过删除该列并再次读取它作为 String 类型来更改学校的类型。

您必须先创建学校对象实例,然后设置学校objectId,然后将学校实例设置为用户的currentSchool。通过这些步骤,您将保存 class 学校的指针给您的 schoolId:

示例:

var SchoolClass = Parse.Object.extend("School");
var schoolObject = new SchoolClass();
schoolObject.set("objectId", schoolId);

Parse.User.signUp(username, password,
{
    firstName: $scope.vm.form.firstName,
    lastName: $scope.vm.form.lastName,
    fullName: $scope.vm.form.firstName + " " + $scope.vm.form.lastName,
    email: email,
    onboardApplicantId: $scope.applicantId,
    currentSchool: schoolObject,
    trainingStatus: "videos",
    videoStatus: 0,
    ACL: new Parse.ACL()
}, {
    success: function (user) {
        $("#signupForm").trigger('reset');
        console.log(' success ');
        $('.signupMsg').text("Account created successfully. Please verify your email address.");
        $('.submit-button').attr("disabled", false);
    },
    error: function (user, error) {
        $('.signupMsg').text(error.message);
        $('.submit-button').attr("disabled", false);
    }
});

好的,我在这里得到了答案:

我在 "Parse.User.signUp" 中这样做:

当前学校: schooolId,

它应该在哪里:

currentSchool: {"__type": "Pointer", "className": "Schools", "objectId": schooolId},

我们还需要传递类名,并且需要通过类型指针定义它是一个关系,它在它们之间创建关系。

我希望这对其他人也有帮助。

谢谢 guyz