Fail to send multiuser email - xpages

Case: i would like to Blind Copy TO multiuser [send mail]
method 1: using vector [Fail] with error message say is null
var maildoc:NotesDocument = database.createDocument();
maildoc.replaceItemValue("Form", "Memo");
maildoc.replaceItemValue("Subject", "STATUS OF APPLICATION FOR REQUEST AN EMAIL");
session.setConvertMime(false);
var z:java.util.Vector = new java.util.Vector();
var vw:NotesView = database.getView("(Notifier Setting)");
var doc:NotesDocument = vw.getFirstDocument();
if (doc != null) {
z.addElement(doc.getItemValue("HRM"));
z.addElement(doc.getItemValue("GM"));
}
maildoc.replaceItemValue("BlindCopyTo",z)
method 2: using array [Fail] with error message replaceitemvalue cannot used array
var z=[];
var vw:NotesView = database.getView("(Notifier Setting)");
var doc:NotesDocument = vw.getFirstDocument();
if (doc != null) {
z.push(doc.getItemValue("HRM"));
z.push(doc.getItemValue("GM"));
}
maildoc.replaceItemValue("BlindCopyTo",z)
method 3:Using string [no person in blindcopy list]
maildoc.replaceItemValue("BlindCopyTo",doc.getItemValue("HRM")+","+doc.getItemValue("GM"))
May i know which way is correct way?

The function NotesDocument.getItemValue() returns a (java.util.)Vector, so if you use addElement or push on z (as in methods 1 and 2), it just adds the whole vector instead of its children.
Your code should work if you use method 1 and replace
z.addElement(doc.getItemValue("HRM"));
z.addElement(doc.getItemValue("GM"));
with
z.addAll(doc.getItemValue("HRM"));
z.addAll(doc.getItemValue("GM"));
PS: Mark Leusink has written a nice SSJS class for mail sending which is available in OpenNTF XSnippets.

Related

Array.push overriding the existing value instead of pushing new value

I am receiving data from the socket server and pushing it into an array. I am also checking if the data I received from the socket exists in an array, If it exists ignore it and if it doesn't push it into an existing array. For some reason though, the socketarray.push part seems to be overwriting previous data in the socketarray variable. No idea what am I doing wrong here
var socketarray = [];
socket.on('data', async data => {
var server_data = data.toString();
var parsed_result = m.parsingData(data.toString());
if (parsed_result) {
var car_id = parsed_result.car_id;
var find_string = socketarray.findIndex(x => x.car_id == car_id);
if (find_string === -1) {
length = socketarray.push({ "car_id": car_id, });
} else {
console.log("already present");
console.log(socketarray);
}
}
});
But there is also one weird thing which is happening. If I add this code
var length = socketarray.push({ "car_id": car_id,});
Before this line var find_string = socketarray.findIndex(x => x.car_id == car_id); which is basically putting a condition to make sure it doesn't save duplicate data. Then multiple data pushed successfully. But the only issue is the duplicity. So I have no idea why the code works before the conditional line and not after it.

how to call the super constructor in a event function

When I run this code I want to be printed as "Mohammed said: hi guys"
but it comes to an error
which is #person is not a function.
Why it is?
var events = require('events');
var util = require('util');
var person = function(name){
this.name = name;
};
util.inherits(person, events.EventEmitter);
var mohammed = new person('mohammed');
var tahir = new person('tahir');
var taha = new person('taha');
var people = ['mohammed', 'tahir', 'taha'];
people.forEach(function(person){
person.on('speak', function(msg){
console.log(person.name + 'said:' + msg);
});
});
mohammed.emit('speak', "Hi guys");
tahir.emit('speak', "i want a chicked");
can anyone fix this? and let me know where I am gone wrong and why?
You're trying to call an .on method on a string in the people array.
You'll need to put the person objects you've created, not strings, into the people array:
var people = [mohammed, tahir, taha];
instead of
var people = ['mohammed', 'tahir', 'taha'];

Return from then - nodejs

I am sort of new to NodeJS and I'm learning as I code but I can't wrap my head around Promise/then.
Here is the piece of code - I'm using a library function to Read database values.
var collection = 'Students';
var query = {};
query.name = 'name';
//readFromDatabse returns -{Function} a promise to return the document found, or null if not found
var temp = readFromDatabase(collection, query).then(function(studentData) {
var result = {
resultDetails: {
username: studentData.username,
password: studentData.password
}
};
return callback(null,resultDetails);
});
but when I read see the values in temp, it contains {"isFulfilled":false,"isRejected":false}!! how can I get the result details into temp?
You have to think of Promises as containers for values. readFromDatabase returns such a container, which might eventually hold the requested value unless the query fails. Your temp variable points to the container, but not the response. The properties isFullfilled and isRejected are attributes of the Promise telling you that it has neither been resolved with a value or rejected with an error.
To get to the response you have to use the then method. The function you register there will be called for you, when the query yields a result or an error.
var collection = 'Students';
var query = {};
query.name = 'name';
var temp = null;
readFromDatabase(collection, query).then(function(studentData) {
var result = {
resultDetails: {
username: studentData.username,
password: studentData.password
}
};
temp = result;
});
// temp is still null here

How to increase performance of redis sub?

I have code like that
var subscribeNewMessages = require("redis").createClient(config.redis.port, config.redis.host);
subscribeNewMessages.subscribe('new-messages');
io.of('/new-messages').on('connection', function (client) {
subscribeNewMessages.on("message", function(channel, message) {
var obj = JSON.parse(message);
if (client.userId == obj.toUserId || client.guestId == obj.toUserId) {
client.send(message);
}
obj = null;
});
})
And how can I optimize it? Because this code parses string json for each new messages.
Also when I try to publish to redis chanel I need to JSON.stringify
redis1.publish(channelPrefix, JSON.stringify(clientData));
There isn't going to be a way to avoid JSON.parse()/JSON.stringify() as long as you're storing js objects. You could use a different serialization format like msgpack, but you're still calling functions to serialize/unserialize your data (also JSON.parse()/JSON.stringify() are already pretty hard to beat performance-wise in node).
I think the only real performance adjustment you could make with the code you've provided is to only parse the JSON once for all clients instead of for each client. Example:
var subscribeNewMessages = require('redis').createClient(config.redis.port, config.redis.host);
subscribeNewMessages.subscribe('new-messages');
var nsNewMsgs = io.of('/new-messages');
subscribeNewMessages.on('message', function(channel, message) {
var obj = JSON.parse(message),
clients = nsNewMsgs.connected,
ids = Object.keys(clients);
for (var i = 0, len = ids.length, client; i < len; ++i) {
client = clients[ids[i]];
if (client.userId == obj.toUserId || client.guestId == obj.toUserId)
client.send(message);
}
});
Depending on your application, you might even be able to avoid the for-loop entirely if you can store the socket.id values in your published messages, then you can simply look up clients[obj.userSockId] and clients[obj.guestSockId] because the connected is keyed on socket.id.

How to disable contexte in a Spotify App (so previous/next buttons are disabled)

Someone ask the same question here. iKenndac answer it. But I need more details and I cannot find a way to add a comment to the question so I create a new question here.
I want to be able to control track played by spotify. So I want to disable previous and next button.
It seems that the solution is to play a track without giving a context to the player. I did it but next/previous are not disabled. Spotify act as the current context is my starred playlist.
Here is some code
var sp = getSpotifyApi(1);
var models = sp.require('sp://import/scripts/api/models');
var player = models.player;
var pl_starred = models.library.starredPlaylist;
exports.init = init;
function init() {
var header = document.getElementById("header");
var index = Math.floor(Math.random()*pl_starred.length)
var track = pl_starred.get(index);
if (pl_starred == null) {
header.innerText = "starred not found";
} else if (track == null) {
header.innerHTML = "cannot get track from starred";
} else {
header.innerHTML = "starred " + index + "/" + pl_starred.length + " : " + track.name;
}
player.play(track,null);
}
Edit: some new test after the answer from iKenndac
I try to separate the track from its context but it does not work.
So I try to load a track using an hardcoded URI. So I did not load the starred playlist. But it did not work too
Here my code
function init() {
var header = document.getElementById("header");
var player = models.player;
models.Track.fromURI("spotify:track:3ZfW4Z3MEvd82pv5J0rN2l", function(trackNotInStarred) {
console.log("Track loaded", trackNotInStarred.name);
player.play(trackNotInStarred, null);
header.innerHTML = "starred: " + trackNotInStarred.name;
});
}
New edit: in fact the previous/next button are not disable but if I click on previous it restart the track and next it jump to a "no track" state and then previous/play/next are disabled.
Any ideas ?
Looks like it's inferring the context from the fact you got the track from the Starred playlist to start with.
Try this to separate the track from the context:
var player = models.player;
var trackInStarred = pl_starred.get(index);
models.Track.fromURI(trackInStarred.uri, function(trackNotInStarred) {
console.log("Track loaded", trackNotInStarred.name);
player.play(trackNotInStarred, null);
});

Resources