TypeError: Cannot read property 'roles' of undefined - node.js

I want the bot to change the color of the roles and it gives an error
client.guilds.get(servers[index]).roles.find('name', config.roleName).setColor(rainbow[place])
^
TypeError: Cannot read property 'roles' of undefined
at Timeout.changeColor [as _onTimeout] (C:\Users\1\Desktop\discord1\Role.js:29:38)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
Here is my code:
function changeColor() {
for (let index = 0; index < servers.length; ++index) {
client.guilds.get(servers[index]).roles.find('name', config.roleName).setColor(rainbow[place])
.catch(console.error);
if(config.logging){
console.log([ColorChanger] Changed color to ${rainbow[place]} in server: ${servers[index]});
}
if(place == (size - 1)){
place = 0;
}else{
place++;
}
}
}

You're getting this error because client.guilds.get('<id>') expects an exact guild id passed in as a String. See the comments in red under the .find method:
All collections used in Discord.js are mapped using their id property, and if you want to find by id you should use the get method. See MDN for details.
Therefore, the data stored at server[index] (in the 3rd line of your code) must not be a valid guild id.

Related

Execute eval on dynamic expression

I have a dictionary that is params["actions"]["browser.evaluateJs"]. It has different expressions that need to be executed. I am trying to execute like this:
if("browser.evaluateJs" in params["actions"]){
for (let key of Object.keys(params["actions"]["browser.evaluateJs"])) {
console.log(params["actions"]["browser.evaluateJs"][key]);
ctx[key] = eval(await params["actions"]["browser.evaluateJs"][key]);
console.log(ctx[key]);
}
}
expression contained in that dictionary is page.waitForXPath('//div[#class='price-text']'). I am trying to scrape a website using pupeteer. And need to get this xpath. But it is giving this error:
UnhandledPromiseRejectionWarning: SyntaxError: missing ) after argument list
How do I fix it?
Edit:
I have used page.evaluate like this:
if("browser.evaluateJs" in params["actions"]){
for (let key of Object.keys(params["actions"]["browser.evaluateJs"])) {
console.log(params["actions"]["browser.evaluateJs"][key]);
ctx[key] = page.evaluate(params["actions"]["browser.evaluateJs"][key]);
console.log(ctx[key]);
}
}
and params["actions"]["browser.evaluateJs"][key] contains element => {return element.textContent;}, (await page.$x('//div[#class='price-text']'))[0]
but this gives Promise { }. How do I fix it?
Edit 2:
I put await like this:
ctx[key] = await page.evaluate(params["actions"]["browser.evaluateJs"][key]);
and it is throwing this error:
Error: Evaluation failed: SyntaxError: Unexpected identifier
Edit3:
Since Im reading it from yml file so I have put it like this:
mykey : "element => {return element.textContent;}, (await page.$x('//div[#class=\"price-text\"]'))[0]"
The error still persists

TypeError: Cannot read property 'Grid' of undefined thrown in Table.forceUpdateGrid

I am attempting to call tableInstance.forceUpdateGrid() inside a Promise.then() callback and it is throwing an exception TypeError: Cannot read property 'Grid' of undefined
Looking at the following code
_createClass(Table, [{
key: 'forceUpdateGrid',
value: function forceUpdateGrid() {
this.Grid.forceUpdate();
}
the this reference is undefined...
the only thing I can think of is that in-between the initial BE api call and the Promise.then() handler, there has been a props change that has caused the containing component to re-render and maybe the tableInstance reference no longer points to the correct instance?
Can anyone help?
(1) Use fat arrow functions to get this reference inside function :-
_createClass(Table, [{
key: 'forceUpdateGrid',
value: forceUpdateGrid = () => {
this.Grid.forceUpdate();
}
(2)Or,
let thisRef = this;
_createClass(Table, [{
key: 'forceUpdateGrid',
value: function forceUpdateGrid() {
thisRef.Grid.forceUpdate();
}
i hope it helps!

Argument "data" is not a valid Document. Input is not a plain JavaScript object

I am getting below error .
Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object.
at Object.exports.(anonymous function) [as isDocument] (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/validate.js:86:15)
at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/write-batch.js:286:14)
at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/reference.js:420:8)
at PainEntryService.createEntry (/user_code/services/entry-service.js:19:30)
at /user_code/services/intentService.js:22:36
at Function.<anonymous> (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:146:23)
at next (native)
at /user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:22:71
at __awaiter (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:18:12)
at Function.handler (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:84:16)
I am using below code for creating the document
var docRef = this.dbManager.collection('tetst).doc(subjectId);
var setAlan = docRef.set(EntryEntity.toJSON);
I have below code in the toJSON method
public toJSON(): testEntry {
let returnJSON = {
"entry_id": this.entry_id,
"subject_id": this.subject_id,
"entry_date": this.entry_date,
"questionnaire": this.questionnaire,
"entry_start_timestamp": this.entry_start_timestamp,
"entry_end_timestamp": this.entry_end_timestamp,
"entry_complete": this.entry_complete,
//"responses": this.responses,
"last_answered_question" : this.last_answered_question,
"entry_status" : this.entry_status
}
return returnJSON;
}
I am framing json object in the above method. If i print the json object, i am getting below content
{ entry_id: 'df2b4ad4-6a70-4304-a71f-3a63773ada61',
subject_id: 'ABwppHHkzfY1Whp-lCHNnvEcuqvsbMKtZsg_ui9vc4jtpXSiAbh0fNsg6LxGkYq-Va3SOrwcvD-HAs7VQA',
entry_date: '2018-06-15',
questionnaire: 1,
entry_start_timestamp: '2018-06-15T09:38:10.266Z',
entry_end_timestamp: '2018-06-15T09:38:10.266Z',
entry_complete: false,
last_answered_question: 0,
entry_status: 'active' }
How to solve above issue? is there any issue with json object?
You need to call the toJSON method. Currently you are passing a reference to a method to set when you need to pass the return value of the called method.
Change
var setAlan = docRef.set(EntryEntity.toJSON);
to
var setAlan = docRef.set(EntryEntity.toJSON());

Cannot read property call of undefined at Blockly.Generator.blockToCode

I am trying to add a custom text block. But when i enter any text in the input field, the error comes up.
"Uncaught TypeError: Cannot read property 'call' of undefined"
Blockly.Blocks['text_input']={
init:function()
{
this.appendDummyInput()
.appendField('Text Input:')
.appendField(new Blockly.FieldTextInput(''),'INPUT');
this.setColour(170);
this.setOutput(true);
}
};
this happens when the language definition for your custom type is missing.
// Replace "JavaScript" with the language you use.
Blockly.JavaScript['text_input'] = function(block) {
var value = Blockly.JSON.valueToCode(block, 'INPUT', Blockly.JavaScript.ORDER_NONE);
// do something useful here
var code = 'var x= "bananas"';
return code;
};

sequelize CreateAssociation return parent instance instead of child created instance

I have an issue with CreateAssociation method.
Here's my code :
User create curves
//User create curves
function saveCurves(user, functions){
var curves = JSON.parse(functions);
for (var i = 0; i < curves.length; i++) {
(function(i,user,curves){
user.createCurve({
i_min: curves[i].i_min,
i_max: curves[i].i_max,
}).then(function(curve){
saveFunction(curve,curves[i].fn,curves[i].variables);
});
})(i,user,curves);
}
}
//save function after creating curves depends on it
function saveFunction(curve,fct,variables){
return curve.createFunction({
fn: fct
}).then(function(math_function){
saveVariables(math_function,variables);
return;
})
}
//save variables to associated function
function saveVariables(fn, variables){
for(var j= 0; j < variables.length; j++){
(function(j,fn,variables){
fn.createVariable({
name: variables[j].name,
function_id: fn.id
})
.then(function(row_variable){
console.log(row_variable.toJSON())
return row_variable.createFunction({
fn: variables[j].fn
})
})(j,fn,variables);
}
I have an issue when I'm calling "fn.createVariable" Method.
I don't really understand why but the promise in .then just after give me fn instance instead of variable instance.
and also I have this error :
Unhandled rejection TypeError: row_variable.createFunction is not a function
When i'm looked into my database, I saw the instance has been correctly inserted
but row_variable in console.log give me fn and not inserted row.
So I cannot understand with I have this return.
I did it well above, so why it doesn't work here ?
(Not relation definition issue because row exist in database)
Any idea please ?

Resources