format date when taken from a constructor - javascript

I want to format the date right but get an error message when i try it.
My code:
var dateText = document.getElementById("text");
var dateDiv = document.createElement("div");
dateDiv.id = "tid";
dateDiv.innerHTML = MessageBoard.messages[messageID].getDateText();
text.appendChild(dateDiv);
And in another .js file:
this.getDate = function() {
return date;
};
this.setDate = function(_date) {
date = _date;
};
/.../
Message.prototype.getDateText = function() {
return this.message.getDate().toLocaleTimeString();
};
But i got an error message that says: "Uncaught TypeError: Cannot call method 'getDate' of undefined"
To get the date with just getDate works fine.

This is just a guess, since you didn't post enough code to tell for sure:
return this.getDate().toLocaleTimeString();

Related

how to pass uint32[], uint8[] parameter to smart contract in web3.js

I'm doing estimateGas. here's my code.
var sdate = new Date('2021-07-01 00:00:00');
var edate = new Date('2021-07-31 00:00:00');
var arrDate = [];
arrDate.push(sdate/1000);
arrDate.push(edate/1000);
var arrCategory = [1,12,14];
var param1 = web3.eth.abi.encodeParameter('uint32[]',arrDate);
var param2 = web3.eth.abi.encodeParameter('uint8[]',arrCategory);
let Contract = new web3.eth.Contract(myPack.abi, myPack.ca);
Contract.methods.createTicket(param1, param2).estimateGas({from: accounts[0]})
.then(console.log);
and I met error
Uncaught TypeError: t.map is not a function
at h.formatParam (index.js:218)
at index.js:100
at Array.map ()
at h.encodeParameters (index.js:94)
at index.js:439
at Array.map ()
at Object.d._encodeMethodABI (index.js:438)
at Object.d._processExecuteArguments (index.js:701)
at Object.d._executeMethod (index.js:720)
at estimateGas (issuecfm:257)
I tried something before encodeParameter
BigNumber
var BN = web3.utils.BN;
arrDate = arrDate.map(item => {return new BN(item)});
arrCategory = arrCategory.map(item => {return new BN(item)});
and String
arrDate = arrDate.map(item => {return item.toString()});
arrCategory = arrCategory.map(item => {return item.toString()});
After a lot of searching, I tried what I could. but I still get the same error. I would really appreciate it if you could teach me how to modify my code.
Using the web3 contract helper function, you need to pass the "raw" JS params, not the ABI-encoded data.
let contract = new web3.eth.Contract(myPack.abi, myPack.ca);
// mind the `arrDate` and `arrCategory` instead of `param1` and `param2`
contract.methods.createTicket(arrDate, arrCategory)
.estimateGas({from: accounts[0]})
.then(console.log);

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined when adding object to field in Sharepoint

I want to fill a nintex form people picker field automatically using JavaScript on a SharePoint page.
The problem is that I keep getting an error:
When I click on the first link, it shows this
In the formMain.js, the error is caused by this line:
ins.add(object);
I've been trying for hours to fix this issue, but I can't find a solution.
As you can see in the console, ins and the object are both defined.
Here's the JavaScript I use to add the user to the field:
var personProperties;
function onChangeProductManagement() {
var curvalue = NWF$('#' + TeilnehmerStatus10).find("input:checked").val();
var ins = new NF.PeoplePickerApi('#' + TeilnehmerName10);
if (curvalue == "offen") {
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
// Make sure PeopleManager is available
SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {
// Replace the placeholder value with the target user's credentials.
var targetUser = "opmain\\eoblae";
// Get the current client context and PeopleManager instance.
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
// Get user properties for the target user.
// To get the PersonProperties object for the current user, use the
// getMyProperties method.
personProperties = peopleManager.getPropertiesFor(targetUser);
// Load the PersonProperties object and send the request.
clientContext.load(personProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
});
});
} else {
console.log("Test2");
console.log("NWF value: " + NWF$('#' + TeilnehmerName10).val());
}
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
var accountName = personProperties.get_accountName();
var displayName = personProperties.get_displayName();
var email = personProperties.get_email();
var object = { value: accountName, label: displayName, type: "user", email: email };
console.log("object: ", object);
var ins = new NF.PeoplePickerApi('#' + TeilnehmerName10);
console.log("ins: ", ins);
ins.add(object);
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
console.log("Error: " + args.get_message());
}
Any help is appreciated!
Adding the id in var object fixed the issue.

RRule JS weekday property not working as expected

I have written following code for getting daily recurrence in my angular, my issue is when I use byweekday property of RRule JS, application stuck and does not respond at all. Sometimes it does work but takes huge time to execute code on line #rrule_after. Please let me know why is this happening and what can be better solutions for this?
function getNextRecurrenceDate(recurrenceVo,rule){
var startDate = new Date(recurrenceVo.recurrenceRangeVO.startDate);
var today = new Date();
var calculatedDate = null;
if(today.getTime() <= startDate.getTime()){
return startDate;
}else{
//#rrule_after - when I debug the code and comes to this function execution, my application stucks/halts for long time and does not response at all
calculatedDate = rule.after(today,true);
// this is temporary code I have written to overcome the problem of daily weekday recurrence
if(calculatedDate && recurrenceVo.dailyRecurrenceVO && recurrenceVo.dailyRecurrenceVO.everyWeekDay && calculatedDate.getDay() > 4){
calculatedDate.setDate(calculatedDate.getDate() + (7 - calculatedDate.getDay()));
}
}
return (calculatedDate)?calculatedDate:'';
}
function getDailyNextRecurrenceDate(recurrenceVo){
var rule = new RRule({
freq: RRule.DAILY,
interval: recurrenceVo.dailyRecurrenceVO.numberofDays,
dtstart: new Date(recurrenceVo.recurrenceRangeVO.startDate)
})
if(recurrenceVo.recurrenceRangeVO.endCount){
rule.options.count = recurrenceVo.recurrenceRangeVO.endCount;
}
if(recurrenceVo.dailyRecurrenceVO.everyWeekDay){
rule.options.interval = 1;
// When I remove this code then all working fine and application not halting at all
rule.options.byweekday = [RRule.MO, RRule.TU, RRule.WE, RRule.TH, RRule.FR]
}
if(recurrenceVo.recurrenceRangeVO.endDate){
rule.options.until = new Date(recurrenceVo.recurrenceRangeVO.endDate);
}
var nextRecDate = getNextRecurrenceDate(recurrenceVo,rule);
return nextRecDate?nextRecDate.setHours(0,0,0,0):'';
}

Error assigning value to JavaScript class property

I am getting an error while setting a class property in javascript class. I am using nodejs prompt module to get user input and setting it to the class property. But i am getting following error.
TypeError: Cannot read property 'resultAge' of undefined
I figured it out that it has something to do with synchronization, but i am not able to figure it out that how to implement it for this situation.
Also i want to prompt user again until he has entered a valid number (I can not use a do while loop, what might be the solution?)
var prompt = require("prompt");
var ageTotal = function(){
this.resultAge = 0;
this.getUserAge = function(){
prompt.start();
//i want to run this until valid input is entered
prompt.get(["age"], function(err, result){
//I know i have to convert userInput to int but thats for later
this.resultAge += result.age
});
}
}
ageTotal.prototype.displayTotalAge = function(){
return this.resultAge;
}
var a = new ageTotal();
a.getUserAge();
var age = a.displayTotalAge();
console.log(age); //This is running before the above function finishes
EDIT:
The problem setting resultAge is solved but now the problem is var age = a.displayTotalAge(); is evaluated after console.log(age) which results in 0;
You need to pass the scope of ageTotal into the prompt.get callback:
var ageTotal = function(){
this.resultAge = 0;
this.getUserAge = function(){
var that = this;
prompt.start();
prompt.get(["age"], function(err, result){
that.resultAge += result.age
});
}
}

Difference of defining a variable inside and outside a function

New to javascript here.
The Team Treehouse blog has a small tutorial on how to build a timer in javascript. It is basically the following code:
<h1 id="timer">Loading</h1>
var updateMessage = function(){
var date = Date();
var message = document.getElementById("timer");
message.innerHTML = "The time is " + date;
}
var timer = setInterval(updateMessage, 500);
This works fine and all. However I want to use date for multiple functions. I tried the following..
var date = Date();
var updateMessage = function(){
var message = document.getElementById("timer");
message.innerHTML = "The time is " + date;
}
..but now it doesn't work realtime. Whenever I hit F5 it gives back the correct time but it's not updated realtime anymore.
Why is this? I thought that when I declare a variable outside of a function, it would become a global function which could be used anywhere.
Because Date() is the time you call it, it does not keep updating!
If you want to share it, update it inside of the function.
var date;
var updateMessage = function(){
date = new Date();
var message = document.getElementById("timer");
message.innerHTML = "The time is " + date;
}
var date = Date();
var updateMessage = function(){
var message = document.getElementById("timer");
message.innerHTML = "The time is " + date;
}
in above case var date is a variable whose value is assigned when script loaded.
and in another case var date is assign all time when the updateMessage is called
var updateMessage = function(){
var date = Date();
var message = document.getElementById("timer");
message.innerHTML = "The time is " + date;
}
var date = Date();
Is only executed once when script loads. The variable date stays the same throughout the lifetime of the webpage. That is why it only changes when you refresh the page.
The problem here is not where you define the date variable but where and how many times you are calling the Date() function.
Declaring it globally but making the necessary call everytime you need is also a valid solution.
var date; // declaration
var updateMessage = function(){
var message = document.getElementById("timer");
date = Date(); // call to Date() everytime updateMessage is executed
message.innerHTML = "The time is " + date;
}
Well you are missing setInterval in second example?
Apart from that, you were on the right track, but with a little problem: The way you are doing it now, you are getting the Date() when you load the page, and just use the same date over and over.
Something like this should work for you, as it would give you a global variable while changing the date dinamically:
var date = Date();
var updateMessage = function(){
date = Date();
var message = document.getElementById("timer");
message.innerHTML = "The time is " + date;
}

Categories