Saving a basic relation in Parse

北战南征 提交于 2019-12-12 01:47:40

问题


The example was straight forward but not surprisingly, it is not working. mLate is an object or array of objects from the Late table. My code:

var user = Parse.Object.extend("User"); 
var userQuery = new Parse.Query(Parse.User);        
var Late = Parse.Object.extend("Late");        
var lateQuery = new Parse.Query(Late);
userQuery.get("yCDDEWoiwM", {//this is where the userID goes
     success: function (userDetails) {
           var relation = userDetails.relation("mLate");
           relation.add(mLate);
           userDetails.save(); 

Given the Parse SDK, I can't imagine where I'm going wrong. I keep getting POST https://api.parse.com/1/classes/_User/yCDDEWoiwM 400 (Bad Request)


A NEW HYPOTHESIS AND MORE CODE

I know this is something really simple and I'm really pissed that Parse so rarely adds even slight detail to what they explain. Have you seen their API (OK, I don't want to get on my soap box . . .)

My hypothesis is that I transformed the Parse.Object from the Late table several times and that somehow Parse no longer knows what's going on there.

Here's a more complete look at the code:

var user = Parse.Object.extend("User"); 
    var userQuery = new Parse.Query(Parse.User);        
    var Late = Parse.Object.extend("Late");        
    var lateQuery = new Parse.Query(Late);
    userQuery.get("yCDDEWoiwM", {//this is where the userID goes
         success: function (userDetails) {
mLate = [];
mLate1 = [];

lateQuery.find({
     success: function (allLate) {
          var aLate = allLate
          for (var i = 0; i < aLate.length; i++){
          var oneLate = aLate[i];
          if(something valid) {
          mLate.push(oneLate)
          }else{
          mLate1.push(oneLate)

var relation = userDetails.relation("mLate");
               relation.add(mLate);
               userDetails.save();

回答1:


You can't extend the user that way. All special classes (User, Installation, Roles, etc) should be addressed like this:

"_User", "_Roles", "_Installation"

so for example:

var user = Parse.Object.extend("_User");

But it's better to reference them in this way:

var user = Parse.Object.extend(Parse.User);

But as far as I can tell you also don't need the first line creating a user object. You also don't need to "extend" an object if all you wanted to do is created a query. Typically "extending" is used if you want to create a pointer but in your case you don't need to.

See if this works for you:

//var user = Parse.Object.extend(Parse.User); 
var userQuery = new Parse.Query(Parse.User);        
//var Late = Parse.Object.extend("Late");        
var lateQuery = new Parse.Query("Late");
userQuery.get("yCDDEWoiwM", {//this is where the userID goes
     success: function (userDetails) {
           var relation = userDetails.relation("mLate");
           relation.add(mLate);
           userDetails.save(); 

If you want to make the query a bit more dynamic you could do this:

var userQuery = new Parse.Query(Parse.User);               
queryQuery.equalTo("objectId", userId); 
userQuery.first().then(function(userDetails){

   var relation = userDetails.relation("mLate");
   relation.add(mLate);

   return Parse.Promise.when(userDetails.save());
}).then( // make your call to response.success ...);

Another possible issue is that your function is returning before your userDetails.save() operation has a chance to finish. If you modify your query to be something like the above to use a Promise then it will guarantee that the async call to save() will actually complete.

What is not shown is the continuation of the promise chain to call response.success or error but you can add that in.




回答2:


Not 100% clear on the intent because the mLate variable is not defined, but you can get a user like this:

var user = new Parse.User({id:"yCDDEWoiwM"});
user.fetch().then(function(user) {
    // add to the relation here, then
    return user.save();
}).then(function () {
    console.log("saved");
});


来源:https://stackoverflow.com/questions/28900094/saving-a-basic-relation-in-parse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!