Joining a 1-to-1 private group in SendBird - javascript

Using the SendBird JavaScript SDK, I am able to correctly create a private group for 1-to-1 messaging:
var params = new sb.GroupChannelParams();
params.isPublic = false;
params.isEphemeral = false;
params.isDistinct = true;
params.addUserIds([1, 2]);
params.operatorIds = [1];
params.name = name;
sb.GroupChannel.createChannel(params, function(groupChannel, error) {
if (error) {
console.log(error);
return false;
}
sb.GroupChannel.getChannel(groupChannel.url, function(groupChannel) {
var userIds = [2];
groupChannel.inviteWithUserIds(userIds, function(response, error) {
if (error) {
console.log(error);
return false;
}
console.log(response);
});
});
});
This all works correctly, and both users are able to see the private chatroom when retrieving the group list. However, when either user attempts to join the private group, an error is encountered:
SendBirdException: Not authorized. "Can't join to non-public
channel.".
To join the group, I'm using the following code:
sb.GroupChannel.getChannel(id, function(openChannel, error) {
if (error) {
console.log(error);
return false;
}
console.log('Channel Found: ' + openChannel.name + '. Current Participants: ' + openChannel.participantCount);
openChannel.join(function(response, error) {
if (error) {
console.log(error);
return false;
}
console.log('Channel Joined: ' + openChannel.name + '. Current Participants: ' + openChannel.participantCount);
// retrieving previous messages.
});
});
The response from the above code is:
{
"message": "Not authorized. \"Can't join to non-public channel.\".",
"code": 400108,
"error": true
}
Any help is much appreciated. Note, I've checked the documentation and it does not mention how to join a private group (only a public one). I don't see how the "creator" of the chatroom is able to join the room, despite being set as the "operator".

It seems like you are trying to join the open channel while it is actually a group channel. You can join the group channel as a member by calling this method call.
if (groupChannel.isPublic) {
groupChannel.join(function(response, error) {
if (error) {
return;
}
});
}
Instead of
openChannel.join(function(response, error) {
if (error) {
console.log(error);
return false;
}
console.log('Channel Joined: ' + openChannel.name + '. Current Participants: ' + openChannel.participantCount);
// retrieving previous messages.
});
https://docs.sendbird.com/javascript/group_channel#3_join_a_channel_as_a_member

Related

How to fix "Error: Can't set headers after they are sent" in Express

I have recently been developing a MERN application and I have recently came into the trouble that express is saying that I am setting headers after they are sent.
I am using mongo db and trying to update a user profile.
I have tried to comment out my res.send points to find the issue but I have failed to do so.
Here is my post method for updating the user profile:
app.post("/api/account/update", (req, res) => {
const { body } = req;
// Validating and Checking Email
if (body.email) {
var email = body.email;
email = email.toLowerCase();
email = email.trim();
body.email = email;
User.find(
{
email: body.email
},
(err, previousUsers) => {
if (previousUsers.length > 0) {
return res.send({
success: false,
message:
"Error: There is already another account with that email address"
});
} else {
}
}
);
}
// Validating Names Function
function checkName(name) {
var alphaExp = /^[a-zA-Z]+$/;
if (!name.match(alphaExp)) {
return res.send({
success: false,
message: "Error: Names cannot contain special characters or numbers"
});
}
}
checkName(body.firstName);
checkName(body.lastName);
// Making sure that all fields cannot be empty
if (!body.email && !body.firstName && !body.lastName) {
return res.send({
success: false,
message: "Error: You cannot submit nothing"
});
}
// Getting User ID from the current session
UserSession.findById(body.tokenID, function(err, userData) {
// Finding User ID using the current users session token
if (userData.isDeleted) {
return res.send({
success: false,
message:
"Error: Session token is no longer valid, please login to recieve a new one"
});
}
// Deleting the token ID from the body object as user table entry doesnt store tokens
delete body.tokenID;
// Finding the user profile and updating fields that are present
User.findByIdAndUpdate(userData.userId, body, function(err, userInfo) {
if (!err) {
return res.send({
success: true,
message: "Success: User was updated successfully"
});
}
});
});
});
This is the call that I am doing to the backend of the site:
onUpdateProfile: function(fieldsObj) {
return new Promise(function(resolve, reject) {
// Get Session Token
const obj = getFromStorage("the_main_app");
// Defining what fields are getting updated
fieldsObj.tokenID = obj.token;
// Post request to backend
fetch("/api/account/update", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(fieldsObj)
})
.then(res => {
console.log("Verify Token - Res");
return res.json();
})
.then(json => {
console.log("Verify Token JSON", json);
if (json.success) {
window.location.href = `/manage-account?success=${json.success}`;
} else {
window.location.href = `/manage-account?success=${json.success}`;
}
});
});
}
Here is my error message that I am getting:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:158:21)
at C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\routes\api\account.js:270:22
at C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\mongoose\lib\model.js:4641:16
at process.nextTick (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\mongoose\lib\query.js:2624:28)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
Can anyone help me with this?
EDIT
I have changed my code, this seems to now work however I feel like its a little messy when put together. Any refactoring tips?
Code:
app.post("/api/account/update", (req, res) => {
// Preform checks on data that is passed through
const { body } = req;
var messages = {
ExistedUser:
"Error: There is already another account with that email address",
NameFormat: "Error: Names cannot contain special characters or numbers",
BlankInputs: "Error: You cannot submit nothing",
accountLoggedOut:
"Error: Session token is no longer valid, please login to recieve a new one",
successfullyUpdated: "Success: User was updated successfully"
};
var usersFound;
if (body.email) {
var email = body.email;
email = email.toLowerCase();
email = email.trim();
body.email = email;
User.find(
{
email: body.email
},
(err, UserCount) => {
usersFound = UserCount;
}
);
}
function capitalize(text) {
return text.replace(/\b\w/g, function(m) {
return m.toUpperCase();
});
}
if (body.firstName) {
body.firstName = capitalize(body.firstName);
}
if (body.lastName) {
body.lastName = capitalize(body.lastName);
}
//Making sure that all fields cannot be empty
if (!body.email && !body.firstName && !body.lastName) {
return res.send({
success: false,
message: messages.BlankInputs
});
}
// Getting User ID from the current session
UserSession.findById(body.tokenID, function(err, userData) {
// Finding User ID using the current users session token
if (userData.isDeleted) {
return res.end({
success: false,
message: messages.accountLoggedOut
});
}
if (userData) {
// Deleting the token ID from the body object as user table entry doesnt store tokens
delete body.tokenID;
// Finding the user profile and updating fields that are present
User.findByIdAndUpdate(userData.userId, body, function(err, userInfo) {
if (userInfo) {
if (!usersFound.length > 0) {
return res.send({
success: true,
message: messages.successfullyUpdated
});
} else {
return res.send({
success: false,
message: messages.ExistedUser
});
}
}
});
}
});
});
You're calling res.send() twice. res.send() ends the process. You ought to refactor such that you call res.write() and only call res.send() when you're done.
This StackOverflow link describes the difference in more detail. What is the difference between res.send and res.write in express?
I believe this is happening, as you're trying to send a response after the first / initial response has already been sent to the browser. For example:
checkName(body.firstName);
checkName(body.lastName);
Running this function twice is going to try and yield 2 different "response" messages.
The product of a single route, should ultimately be a single response.
Thanks for all your help on this issue.
Here is my final code that allowed it to work.
I have also tried to "refactor" it too. Let me know if you'd do something else.
app.post("/api/account/update", (req, res) => {
const { body } = req;
console.log(body, "Logged body");
// Defining objects to be used at the end of request
var updateUserInfo = {
userInfo: {},
sessionToken: body.tokenID
};
var hasErrors = {
errors: {}
};
// Checking that there is at least one value to update
if (!body.email && !body.firstName && !body.lastName) {
var blankError = {
success: false,
message: "Error: You cannot change your details to nothing"
};
hasErrors.errors = { ...hasErrors.errors, ...blankError };
} else {
console.log("Normal Body", body);
clean(body);
console.log("Cleaned Body", body);
updateUserInfo.userInfo = body;
delete updateUserInfo.userInfo.tokenID;
}
// Function to check if object is empty
function isEmpty(obj) {
if (Object.keys(obj).length === 0) {
return true;
} else {
return false;
}
}
// Function to remove objects from body if blank
function clean(obj) {
for (var propName in obj) {
if (obj[propName] === "" || obj[propName] === null) {
delete obj[propName];
}
}
}
// Checking and Formatting Names Given
function capitalize(text) {
return text.replace(/\b\w/g, function(m) {
return m.toUpperCase();
});
}
if (body.firstName) {
body.firstName = capitalize(body.firstName);
}
if (body.lastName) {
body.lastName = capitalize(body.lastName);
}
// Checking and formatting email
if (body.email) {
body.email = body.email.toLowerCase();
body.email = body.email.trim();
// Checking for email in database
User.find({ email: body.email }, (err, EmailsFound) => {
if (EmailsFound.length > 0) {
var EmailsFoundErr = {
success: false,
message: "There is already an account with that email address"
};
hasErrors.errors = { ...hasErrors.errors, ...EmailsFoundErr };
}
});
}
// Getting User Session Token
UserSession.findById(updateUserInfo.sessionToken, function(err, userData) {
// Finding User ID using the current users session token
if (userData.isDeleted) {
var userDeletedError = {
success: false,
message:
"Your account is currently logged out, you must login to change account details"
};
hasErrors.errors = { ...hasErrors.errors, ...userDeletedError };
} else {
// Finding the user profile and updating fields that are present
User.findByIdAndUpdate(
userData.userId,
updateUserInfo.userInfo,
function(err, userInfo) {
// userInfo varable contains user db entry
if (err) {
var updateUserError = {
success: false,
message: "Error: Server Error"
};
hasErrors.errors = {
...hasErrors.errors,
...updateUserError
};
}
if (isEmpty(hasErrors.errors)) {
res.send({
success: true,
message: "Success: You have updated your profile!"
});
} else {
res.send({
success: false,
message: hasErrors.errors
});
}
}
);
}
});
});

Using Graph API not able to get events for particular date using filter param

var authEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?";
var redirectUri = "http://localhost:8080";
var appId = "SomethingSomething";
var scopes = "openid profile User.Read Mail.Read Calendars.Read";
function getUserEvents(callback) {
getAccessToken(function(accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: done => {
// Just return the token
done(null, accessToken);
}
});
// Get the 10 newest events
client
.api("/me/events")
.filter("startDateTime='2018-03-01'&endDateTime='2018-03-31'")
.select("subject,start,end,createdDateTime")
.orderby("createdDateTime DESC")
.get((err, res) => {
if (err) {
callback(null, err);
} else {
callback(res.value);
}
});
} else {
var error = { responseText: "Could not retrieve access token" };
callback(null, error);
}
});
}
This is the request that my program sends out:
https://graph.microsoft.com/v1.0/me/events?$filter=2018-07-15T01:00:00&$select=subject,start,end,createdDateTime&$orderby=createdDateTime%20DESC
400 (Bad Request)
You're attempting to filter the /events endpoint using properties that don't exist in the event object (startDateTime and endDateTime). You're also passing a = in the filter clause instead of eq.
The /calendarView endpoint does have startDateTime and endDateTime parameters, but these are query params unto themselves (not part of a filter clause). I suspect this is the operation you're actually looking for:
client
.api("/me/calendarview")
.query({
startdatetime: "2018-03-01T00:00:00.0000000",
enddatetime: "2018-03-31T23:00:00.0000000"
})
.select("subject,start,end,createdDateTime")
.orderby("createdDateTime DESC")
.get((err, res) => {
if (err) {
callback(null, err);
} else {
callback(res.value);
}
});

Parse updating a set of objects

I have trouble updating a set of values in my cloud code. I have tried .save() seperately and .saveAll() but the class doesn't get updated in Parse and I get errors returned.
What I am trying to do is to get all messages from class ChatMessages which has a pointer to the Parse user and Chat class. When the method is called, the class column readAt needs to be updated to the current date. I call my method from an iOS (objective-C) app.
This is the latest version of my method:
Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {
var errorMsg;
var roomName;
var _ = require('underscore.js');
var userPointer = new Parse.User.current();
if (!request.params.roomName) {
errorMsg = "Chat room needs to be identified";
} else {
roomName = request.params.roomName;
}
console.log("Checking chats for userID: " + userPointer.id);
if (!userPointer.id) {
var emptyUserMsg = "User has to be provided";
if (errorMsg) {
errorMsg = errorMsg + emptyUserMsg;
} else {
errorMsg = emptyUserMsg;
};
}
if (errorMsg) {
response.error(errorMsg);
}
var chatQuery = new Parse.Query("Chat");
chatQuery.equalTo("roomName", roomName);
chatQuery.find({
success: function(results) {
if (results.length > 0) {
var chat = results[0];
console.log("Found chat with ID: "+chat.id);
var chatMessagesQuery = new Parse.Query("ChatMessage");
chatMessagesQuery.equalTo("chat", chat);
chatMessagesQuery.notEqualTo("fromUser", userPointer);
chatMessagesQuery.equalTo("readAt", undefined);
chatMessagesQuery.find().then(function(chatMessagesQueryResults) {
_.each(chatMessagesQueryResults, function(result) {
result.set("readAt", new Date());
console.log("Setting readAt for chat message " + result.id + " which has toUser " + result.get("toUser"));
});
return Parse.Object.saveAll(chatMessagesQueryResults,{
success: function(list) {
console.log("Success updating objects");
},
error: function(error) {
console.log("Error updating objects: " + error);
},});
}).then(function(results) {
response.success(results);
console.log("Update for reatAt for chat is successfull");
}, function(error) {
response.error(error);
console.log(error);
});
} else {
response.error("No rooms found");
console.log("No rooms found");
}
},
error: function(error) {
response.error("Room name not found");
console.log(error);
}
});
});
Log output:
E2015-07-19T09:13:48.483Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
Result: {"code":101,"message":"object not found for update"}
I2015-07-19T09:13:48.540Z]Checking chats for userID: CZwQL4y751
I2015-07-19T09:13:48.593Z]Found chat with ID: gfvAkirqTs
I2015-07-19T09:13:48.647Z]Setting readAt for chat message ZiWUIdUtUm which has toUser undefined
I2015-07-19T09:13:48.648Z]Setting readAt for chat message YHEBLpR04U which has toUser undefined
I2015-07-19T09:13:48.649Z]Setting readAt for chat message 0wZ4LQd8ZC which has toUser undefined
I2015-07-19T09:13:48.650Z]Setting readAt for chat message MYsYGyXI0k which has toUser undefined
I2015-07-19T09:13:48.751Z]Error updating objects: [object Object]
I2015-07-19T09:13:48.752Z]{"code":101,"message":"object not found for update"}
E2015-07-19T09:13:49.042Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
Result: {"code":101,"message":"object not found for update"}
Class:
The query can be vastly simplified by making a chatMessages query relational to chats matching the user and room criteria. The code structure can be improved by not mixing callback and promise styles, and by separating logical chunks into small, promise-returning functions.
Stripping away some of the debug instrumentation you added, we get (untested, of course)...
function unreadChatMessagesInRoom(roomName, excludeUser) {
var query = new Parse.Query("ChatMessage");
query.notEqualTo("fromUser", excludeUser);
query.doesNotExist("readAt");
var chatQuery = new Parse.Query("Chat");
chatQuery.equalTo("roomName", roomName);
query.matchesQuery("chat", chatQuery);
return query.find();
}
Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {
var _ = require('underscore.js');
var user = request.user;
unreadChatMessagesInRoom(request.params.roomName, user).then(function(chatMessages) {
console.log(chatMessages.length + " chat messages found");
_.each(chatMessages, function(chatMessage) {
chatMessage.set("readAt", new Date());
});
return Parse.Object.saveAll(chatMessages);
}).then(function(results) {
response.success(results);
}, function(error) {
response.error(error);
});
});

How to Delete Item from Object in NodeJS/Mongo Service

I'm querying for a user object and performing a series of updates on the items within compared to incoming postdata (jsObject). I'm wondering how to completely remove an item from the object...specifically a Date object (user[0].birthDate)...before I make a save of the updated user object.
orm.User.find({ appId: appId, facebookUsername:usersFBUsername}).exec(function (error, user) {
if (error) {
console.log('Error in User Query: ' + error);
}
else if(Object.keys(user).length > 0) {
if(jsObject.name != null)
user[0].name = jsObject.name;
if(jsObject.email != null)
user[0].emailAddress = jsObject.email;
if(jsObject.birthDate != null && jsObject.birthDate.length > 0) {
user[0].birthDate = jsObject.birthDate;
}
else {
console.log('delete it');
//orm.User.update({_id:user._id}, {$pull:{birthDate:1}});
//delete user[0].birthDate;
}
}
user[0].save(function (error) {
if (error != null) {
console.log('An error has occurred while saving user:' + error);
response.end(results.getResultsJSON(results.ERROR, error));
}
else {
console.log(' [User Successfully Updated]');
response.end('{ "success": ' + JSON.stringify(user[0]) + ' }');
}
});
});
You can see in the commented code some attempts I've made which have not been successful. I even gave this a try after the save completed, which also did not work:
orm.User.update({appId: appId, facebookUsername:usersFBUsername},{$pull:{birthDate:deleteBirthDate}})
.exec(function(error){
if(error) {
console.log('oh well: ' + error);
}
else {
console.log('maybe maybe');
}
});
I appreciate any suggestions.
Chris
$pull is for removing values from arrays, but you can use $unset:
orm.User.update(
{_id : user._id},
{ $unset : { birthDate : 1 }},
function(err, numAffected) {
...
}
);
For reference:
https://docs.mongodb.com/manual/reference/operator/update/unset/

Mongoose / JavaScript - Saving error - undefined object

I am trying to save a match between 2 teams, I am passing the 2 teams through a drop down list.
When I use util.log to output the homeTeam to the console when INSIDE the Team.findByKey method it works successfully and here is the output:
3 Mar 19:52:33 - { name: 'Liverpool',
_id: 51312074bb176ba624000007,
__v: 0,
key: 1362174068837 }
But as soon as I try to do this outside of this method I get the following output which means that when I try to save this as a match, the hometeam appears as just undefined rather than the id of the hometeam:
3 Mar 19:54:09 - [object Object]
My problem is that I am eventually wanting to save both a home team and an away team to the same match in one save. The code for saving a match works when inside the Team.findByKey method which is as follows:
app.get('/save/matchTest', function(req, res) {
var key = 1362174006191; // Man Utd 51312036bb176ba624000001
Team.findByKey(key, function(err, team) {
util.log(team);
if(err) {
util.log("Error occured");
}
if(!team) {
util.log("The team does not exist");
}
var match = new Match({
hometeam: team._id
});
match.save(function(err) {
if(err) {
util.log('Error while saving Match: ' + util.inspect(err));
res.send("An error occured whilst saving the match");
} else {
res.send("Saved the match");
}
});
});
});
But what I want to do is to be able to save a match with the following
var match = new Match({
hometeam: homeTeam._id,
awayteam: awayTeam._id
});
Does anyone have any ideas?
Here is the relevant code:
JavaScript
submitMatch = function(){
var homeId = $("#homeTeamList").val();
var awayId = $("#awayTeamList").val();
//alert("home: " + homeId + " away: " + awayId);
// Frontend sends the data
var matchForm = {
homeKey : $('#homeTeamList').val(),
awayKey : $('#awayTeamList').val()
};
// Basic validation
$.post('/save/match', {'matchForm' : matchForm}, function(response) {
console.log(response);
});
};
/save/match
app.post('/save/match', function(req, res) {
util.log('Serving request for url [GET] ' + req.route.path);
// Output to console to test what is being passed to Save Match
// Entire body passed to console
//console.log('body: ', req.body);
// Entire matchForm from body
//console.log('matchForm: ', req.body.matchForm);
// Home Key from matchForm
//console.log('homeKey: ', req.body.matchForm.homeKey);
// Away Key from matchForm
//console.log('awayKey: ', req.body.matchForm.awayKey);
// Get data from match Form
var matchForm = req.body.matchForm;
// Check if a match with 2 teams has been submitted
if(matchForm.homeKey === '' || matchForm.homeKey === undefined ||
matchForm.awayKey === '' || matchForm.awayKey === undefined){
// Not a valid match
util.log('Not valid match');
} else {
var homeId = matchForm.homeKey;
var awayId = matchForm.awayKey;
var homeTeam = Team.findByKey(homeId, function(err, homeTeam) {
util.log(homeTeam);
if(err) {
util.log("Error occured");
}
if(!homeTeam) {
util.log("The home team does not exist");
}
});
var match = new Match({
hometeam: homeTeam._id
});
//util.log(match);
}
});
In /save/match you're using the value of homeTeam in the Match constructor before it's been set by the callback. You need to create the Match inside both the home and away team findByKey callbacks like this:
Team.findByKey(homeId, function(err, homeTeam) {
util.log(homeTeam);
if(err) {
return util.log("Error occured");
}
if(!homeTeam) {
return util.log("The home team does not exist");
}
Team.findByKey(awayId, function(err, awayTeam) {
util.log(awayTeam);
if(err) {
return util.log("Error occured");
}
if(!awayTeam) {
return util.log("The away team does not exist");
}
var match = new Match({
hometeam: homeTeam._id,
awayteam: awayTeam._id
});
});
});
To look up the home and away teams in parallel while still keeping your code organized, you'll want to look at using a flow control library like async.

Categories

Resources