Cloud Code to update User obj doesn't work, fails silently

Hi all,

When i use the following cloud code to update a user (after locating by objectId), it will successfully return the user, but it will never save. The save code i have is below. Iā€™ve tried all angles i feel like, and itā€™s weird because not even the logging statements below log to the moralis logger.

Moralis.Cloud.define("TrackProgress", (request) => {
  	
    let userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("objectId", request.params.user_id);  
    userQuery.first({useMasterKey: true,
      success: function (user) {
        logger.info("user: ", user);
        user.set("email", "test");
        user.save(null, { useMasterKey: true });
      },
      error: function (error) {
        logger.info("error: ", error.message);
      },
  	});
     
});

Here you have to use + between parameters, logger.info logs only the first parameter

it may fail here if it tries to check fi the email is valid

you could try first with another field

I tried with updating a different field as well and no luck either

it looks like cloud code on your server doesnā€™t work now (because of syntax error somewhere)

can you run a simpler cloud function that only logs something?

Hi @snuffle

Try this

 user.save(null, { useMasterKey: true }).then((monster) => {
  // The object was saved successfully.
}, (error) => {
  // The save failed.
  // error is a Moralis.Error with an error code and message.
});

Now what I have is the following, and nothing logs to the moralis logs, nor saves :confused: The fields ā€œfullNameā€ is just a normal string field.

Moralis.Cloud.define("TrackProgress", (request) => {
  	
    let userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("objectId", request.params.user_id);  
    userQuery.first({useMasterKey: true,
      success: function (user) {
        user.set("fullName", "test");        
        user.save(null, { useMasterKey: true }).then((result) => {
          // The object was saved successfully.
          logger.info("success saving user");
        }, (error) => {
          // The save failed.
          // error is a Moralis.Error with an error code and message.
          logger.info("error saving user");
        });
      },
      error: function (error) {
        logger.info("error grabbing user");
      },
  	});
     
});

Try to add a log line on the first line of the function. To know at least that it was called.

@cryptokid with the below code, only the line ā€œgot hereā€ is logged in moralisā€™ dashboardā€¦

Moralis.Cloud.define("TrackProgress", (request) => {
  	
    logger.info("got here");
    let userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("objectId", request.params.user_id);  
    userQuery.first({useMasterKey: true,
      success: function (user) {
        logger.info("got here 2");
        user.set("fullName", "test");        
        user.save(null, { useMasterKey: true }).then((result) => {
          // The object was saved successfully.
          logger.info("success saving user");
        }, (error) => {
          // The save failed.
          // error is a Moralis.Error with an error code and message.
          logger.info("error saving user");
        });
      },
      error: function (error) {
        logger.info("error grabbing user");
      },
  	});
     
});

Can you try to use try to see if there is any exception?

You can also try a syntax with await

You can also log this user_id value to see if it is the expected value

@cryptokid when i log out user id, it logs just fine, again, no errors even logged with below codeā€¦

and even when i wrap the userQuery.first in a try/catch block, it never even get to the catchā€¦ fails silently

Moralis.Cloud.define("TrackProgress", (request) => {
  	
    logger.info("got here");
    let userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("objectId", request.params.user_id);  
    logger.info("user id " + request.params.user_id);
    userQuery.first({useMasterKey: true,
      success: function (user) {
        logger.info("got here 2");
        user.set("fullName", "test");        
        user.save(null, { useMasterKey: true }).then((result) => {
          // The object was saved successfully.
          logger.info("success saving user");
        }, (error) => {
          // The save failed.
          // error is a Moralis.Error with an error code and message.
          logger.info("error saving user");
        });
      },
      error: function (error) {
        logger.info("error grabbing user");
      },
  	});
     
});

if you comment this line, then it works?

instead of objectId can you try with id?

this works fine for me:


Moralis.Cloud.define("TrackProgress", async (request) => {
  	
    logger.info("got here");
    q = new Moralis.Query("User");
    q.equalTo("objectId", "oDKz3DFG8Apj6gAxQmvkIsZU");  
    x = await q.find({useMasterKey:true})
    logger.info(JSON.stringify(x))
    return x
});

@cryptokid iā€™ve gotten code to return back the found user before, but never been able to actually save any updates to that objectā€¦

this updates the user object without problems:

Moralis.Cloud.define("TrackProgress", async (request) => {
  	
    logger.info("got here");
    q = new Moralis.Query("User");
    q.equalTo("objectId", "oDKz3DFG8Apj6gAxQmvkIsZU");  
    x = await q.find({useMasterKey:true})
    logger.info(JSON.stringify(x))
    x[0].set("username", "new_username")
    x[0].save(null, {useMasterKey:true})
    return x
});

@cryptokid the code still doesnā€™t seem to work, so weird!

this code:

// find user
    let userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("objectId", request.params.user_id);  
    results = await userQuery.find({useMasterKey:true});
    logger.info(JSON.stringify(results));
	
    // update user and save
    results[0].set("username", "test");
    results[0].save(null, {useMasterKey:true});
    return results;

gives me this error:

Error: Invalid function: "TrackProgress"
    at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:126:13)
    at /moralis-server/lib/PromiseRouter.js:85:20

you can get that error if the code didnā€™t compile (because of an error somewhere) or the server was updating at that time
also make sure to use async in async (request)

@cryptokid ah that did it, it was the missing async in the method definition. Now i can save and update string values, but objects wonā€™t work and i cannot tell what to do from the docs. Iā€™ve tried saving an object field in the database with both {} and ā€œ{}ā€