get method not working on Parse current User - javascript

I am making an express app with Parse. In my cloud code, I am trying to get an attribute of the current user, but it is returning me undefined. My code looks like following:
app.get('/home/subscriptions',function(req,res){
Parse.Cloud.useMasterKey();
var user = Parse.User.current();
var ifstud = user.get("isStudent");
console.log('student: ' + ifstud); // undefined
console.log('id: ' + user.id); // OK. works fine.
}
I am able to retrieve the id of the user as above but not able to call the get method on user. In their API reference, they have mentioned that Parse.User.current() returns a Parse.Object, so I think in user I have _User object and I should be able to call all methods supported by a Parse.Object.
What might be the issue here?
Thanks

I figured it out and putting this answer for future reference to users who visit this question.
The Parse.User.current() returns only a pointer to the user and not the complete user object. To get access to all fields of the user, fetch the entire object using the fetch method.
var fullUser;
Parse.User.current.fetch().then(user){
fullUser = user;
}).then(function(){
// Place your code here
});

I think it should be: var ifstud = Parse.User.get("isStudent");

Related

Trying to get a snapshot of a variable from firebase gives me an error

Problem
In a social media app I am making with react native and firebase, I am trying to grab the number of comments a post has using the snapshot function of a variable I have saved on my servers, then I am going to add one to this variable when a user adds a new comment. My code to do so is right here:
firebase.database().ref('posts').child(this.state.passKey).update({
comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
})
When I actually run this code, I get an error saying:
Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["
At first I thought this might be that the "this.state.passKey" wasn't actually passing the key, but putting in a key I copied from the server didn't fix the problem.
My Server
-
To get the comments of particular post you should do like this
let postId='someId'
postRef=`/posts/${postId}`
firebase.database().ref(postRef).once("value", dataSnapshot => {
comment=dataSnapshot.val().comments
});
It looks like you're expecting this bit of code to query the database:
firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
Unfortunately, it doesn't work that way. There's no snapshot property on a database Reference object returned by child() or ref().
Instead, you'll need to query the database at that reference, then when you're called back with its value, you can apply it elsewhere.
var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
// use the snapshot here
})

Updating in vtiger from Javascript. Permission to perform the operation is denied for id

I have a node application that needs to be integrated into vtiger, and I have successfully been able to create, delete and retrieve information from my vtiger instance. If I try to update however, I get a Permission to perform the operation is denied for id error.
I have tried a couple different methods i.e. different ways of performing the request. And to test it at the moment I am pulling all of the data (result in the below code) for an id, changing one value and then calling the update using:
var requestJS = require('request');
//Real result comes stright from CRM, but an example of what is being passed through
result = {
'lastname': 'Updated last name',
'id': '12x10',
'assigned_user_id': '19x5',
}
var url = VT_URL + '?operation=update&sessionName=' + session + '&element=' + encodeURIComponent(JSON.stringify(result));
requestJS.post(url, function(err, res, body){
//stuff here
});
I have also tried by attaching the result as the body, and by not using the encodeUriComponent function. Always the same error.
where VT_URL is my vitger url and session is my session id retrieved from login.
I am using the credentials of an admin so I should have read/write access to contacts in the CRM instance.
I have been stuck on this for a while and can't find an answer
So it's not really an answer, but as I changed to a new vtiger instance it all seemed to work fine. So I'm assuming it was more to do with the installation of vtiger rather than an error in the code.
Thought I would keep this question here though because I've seen it around a fair bit
Can you check on your previous vtiger instance if there is an entry (in the database) for your module (I assume Contacts) in the vtiger_ws_entity table ?
If yes, ID is 12 ?

afterSave member Parse Cloud Code not saving

I am attempting to save a username and userId after a user registers into a Runner Class within Parse. For some reason the information is not saving and I am not receiving an error. Can anyone give some advice?
Parse.Cloud.afterSave(Parse.User, function(request){
if(!request.object.existed()){
var RunnerClass = Parse.Object.extend("Runner");
var runner = new RunnerClass();
runner.set("username", request.object.get("username"));
runner.set("userId", request.object.id);
runner.save();
}
});
Your afterSave is incorrectly defined. There is no response for afterSave. Only on beforeSave.
Parse.Cloud.afterSave(Parse.User, function(request) {
...
}
You have to avoid using request.object.existed() in your afterSave trigger because it currently always returns false. This is unfortunately due to a known bug in Parse Cloud which is yet to be fixed. Instead you have to use the workaround given here in the bug report discussion: https://developers.facebook.com/bugs/1675561372679121/
The workaround is to replace request.object.existed() with the following boolean in your afterSave:
var objectExisted = (request.object.get("createdAt").getTime() != request.object.get("updatedAt").getTime());
Also make sure to use request.user instead of request.object in your code

How to get userIdentity from inside javascript adapter?

I am on MobileFirst 7.1, and I am trying to do something similar to this: Get user id from inside a protected adapter but this time is in javascript.
I have followed the tutorial and the protected procedure triggers login, I have made sure that application-descriptor.xml contains <userIdentityRealms>MyRealm</userIdentityRealms> however user identity is null(again).
How can I get the user identity from inside the following procedure?
function myProcedure() {
// I want to get the userid this Java into Javascript,
// SecurityAPI security = serverAPI.getSecurityAPI();
// OAuthUserIdentity identity = security.getSecurityContext().getUserIdentity();
// String userid = identity.getId();
var userid = ???
var facade = new com.ibm.jp.facade.SomeFacade();
var list = facade.SomeMethod(userid);
return JSON.parse(list);
}
In the beginning I was trying to get the user identity from inside the Java facade but it is null. I suspect it is not in the same context? That is why I am trying to get it from the js adapter and pass it as a parameter of someMethod(). If there is a better way to get it I would like to know.
I found it!
var user = WL.Server.getActiveUser();
var userid = user.userId;
...

Finding a user by ID inside a collection.allow behaving very strangely in Meteor

I only want to allow a user to insert a document when his email is verified, I wrote the following code.
Events.allow({
insert: function (userId, doc) {
var user = Meteor.users.find({_id: userId});
console.log(user); // logs a large object
// if the user's email is verified
if (user.emails[0].verified) {
return true;
}
return false;
}
});
Running this, I get an error, "internal server error", looking at the server, I get TypeError: Cannot read property '0' of undefined, which means that something is wrong with my user object. So I logged the object, and instead of seeing a user, I get a huge object, abbreviated below:
{ _mongo:
// lots of stuff
}
I think that's the top level mongoDB object that Meteor uses.
How in the world is Meteor.users.find({_id: userId}) returning the MongoDB object instead of the user I'm looking for?
You're probably looking for findOne, and not find. find returns a cursor to the result, findOne returns the first matched document.

Categories

Resources