Parse-sdk-js: You cannot add or remove an unsaved Parse Object from a relation

Created on 20 May 2018  Â·  14Comments  Â·  Source: parse-community/Parse-SDK-JS

Hi everyone,

I'm trying to add relation between my Place & User classes. But getting "You cannot add or remove an unsaved Parse Object from a relation" error. My code is like below. What am I doing wrong? Could you help please

var user = req.session.user;
var placeId = req.body.placeId;

  var placeQuery = new Parse.Query('Place');
  placeQuery.equalTo('objectId', placeId);
  placeQuery.first()
    .then(function (place) {
      place.increment('likeCount');
      var relation = place.relation('likes');
      relation.add(user);
      return place.save(null, {useMasterKey : true})
    })
    .then(function () {
      return next();
    })
    .then(function (error) {
      return res.status(500).send(error);
    })

All 14 comments

it Seems to be a typo. User vs user. Also, can you try to log the user objectId? How do you get req.session.user?

@flovilmart here is user's objectId. I'm getting req.session.user by Parse.User.logIn
image
image

You’re calling logIn on the server? This is discouraged and you should probably use the become to transform a session token into a user, but I digress. I’m not sure what yields the error, JS or server.

can you try to use a pointer instead of the User from the login?

@flovilmart sorry. I'm beginner at parse and I dont understand what do you mean with this => "can you try to use a pointer instead of the User from the login?"

First im not sure how you manage to login your user, what’s the context of your function etc, how you access / get your user with a login call,m etc...

Can you provide more context please?

Also, a valid object should have the id property set not the objectId one

@flovilmart here is how I'm authenticating user & adding to session.

var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

var api = new ParseServer({
  databaseURI: databaseUri,
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID,
  masterKey: process.env.MASTER_KEY,
  serverURL: process.env.SERVER_URL,
  liveQuery: {
    classNames: ["Posts", "Comments"] 
  }
});
app.post('/login', function (req, res) {

    var username = req.body.username;
    var password = req.body.password;

    Parse.User.logIn(username, password).then(function (user) {
        req.session.user = user;
        req.session.token = user.getSessionToken();
    }, function (error) {

    });
});

And then? How is the user maintained on the session object? As a Parse object? Can you do console.log(req.session.user);

And check req.session.user.constructor.name

My gut feeling is that somehow your user is serialized when written / read from req.session and not a Parse.Object anymore

@flovilmart
image
image

There you have your issue, your object is not a Parse.Object but the JSON representation.

You have two options:

function getUser(session) {
   if (!session.user) { return; }

   return Parse.User.fromJSON(Object.assign({}, session.user, { className: '_User' }));
}

function getUserPointer(session) {
    if (!session.user) { return; }
    const user = new Parse.User();
    user.id = session.user.objectId;
    return user;
}

and then you can use them:

const user = getUser(req.session);
const pointer = getUserPointer(req.session);

/* your code */

      var relation = place.relation('likes');
      relation.add(user);
      // OR relation.add(userPointer);
      return place.save(null, {useMasterKey : true})

@flovilmart thanks for reply. one more question. how can I check relation contains user or not ?

You can make a query on the Relation, (.relation(‘likes’).query()) And check that the user is not part of it

query.get(user.id) 

Or use the .equalTo(‘objectId’, user.id)

If it gives a result, this means the user is already in the relation.

@flovilmart thank you very much. you save me :)

Hah good luck and good weekend!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RaschidJFR picture RaschidJFR  Â·  7Comments

Gyran picture Gyran  Â·  5Comments

futureinapps picture futureinapps  Â·  3Comments

myuller picture myuller  Â·  5Comments

marktyers picture marktyers  Â·  3Comments