Variable when passed through a function returns property of value undefined. I'm passing variable ApiId or ApiName. Should the variable name be wrapped some other way inside the function?
I'm using this answer method and using it inside a function.
function dimensionValue(name) {
console.log("Testing")
return sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
}
If I try to console.log then it works fine.
console.log(sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId').value)
Error:
ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'value' of undefined",
"stack": [
"TypeError: Cannot read property 'value' of undefined",
" at dimensionValue (/var/task/index.js:20:80)",
" at Runtime.exports.handler (/var/task/index.js:37:36)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
code:
function dimensionValue(name) {
console.log("Testing")
//var dimensionsValue = sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
return sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
}
if (sns.Trigger.Namespace == "AWS/ApiGateway") {
console.log("Testing2")
if (sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId') && sns.Trigger.Dimensions.find(dimension => dimension.name === 'Stage')) {
console.log('ApiId and Stage')
var sns_DimensionsValue = dimensionValue('ApiId') + '_' + dimensionValue('Stage')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName') && sns.Trigger.Dimensions.find(dimension => dimension.name === 'Stage')) {
console.log('ApiName and Stage')
var sns_DimensionsValue = dimensionValue('ApiName') + '_' + dimensionValue('Stage')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId')) {
console.log('ApiId')
var sns_DimensionsValue = dimensionValue('ApiId')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName')) {
console.log('ApiName')
var sns_DimensionsValue = dimensionValue('ApiName')
}
}
The problem are those sns_DimensionsValue == parts at the start of every condition. Just remove them.
You essentially do if (undefined == findAppId() && findStage()), which will be true if Stage was found and AppId wasn't, but then you attempt to access the value of AppId and fail.
Let me suggest a generally simplified approach:
const dimensions = Object.fromEntries(sns.Trigger.Dimensions.map(d => [d.name, d.value]))
let sns_DimensionsValue = dimensions.ApiId ?? dimensions.ApiName
if (!sns_DimensionsValue) throw new Error('No API ID/API name found')
if (dimensions.Stage) sns_DimensionsValue += '_' + dimensions.Stage
Related
hi i am trying to implement a local orderbook using the binance api however i keep getting this error every now and then it does not always happen but if it does it will happen early on please help
this is the main file that runs calling an exports function from an external file to process the data coming from the web socket on message
wsClient.subscribeSpotDiffBookDepth("btcusdt");
wsClient.on('message', (data) => {
// Setup and process order book information
order_book.update_orderbook(data);
if(order_book.ready === 1){
order_book.get_orderbook_history();
}
});
wsClient.on('error', err => {
/* handle error */
console.log("this is it 2 " + err);
});
wsClient.on('close', () => {
/* ... */
});
exports function in external file
exports.update_orderbook = function(data) {
if(data.e == "depthUpdate"){
if(this.ready === 0){
this.ready = 1;
console.log("Stage 1 in play");
}else{
this.buffer += data;
}
if(this.ready === 2 && this.asks != null && this.bids != null){
if(undefined !== this.asks && undefined !== this.bids && undefined !== data.a && undefined !== data.b){
if(data.U <= this.lastUpdateId + 1 && data.u >= this.lastUpdateId + 1){
// error is coming from calling this function-------------------------------------------------------
var temp_array1 = sort_array(this.asks, data.a);
var temp_array2 = sort_array(this.bids, data.b);
this.asks = temp_array1;
this.bids = temp_array2;
this.lastUpdateId = data.u;
console.log("Stage 3");
}
}
}else{
this.buffer += data;
}
}
}
function to sort and update the array
function sort_array(array1, array2){
for(let x in array2){
if(array2[x][1] == 0){
for(let i in array2){
if(array1[i][0] === array2[x][0]){
array1.splice(i, 1);
}
}
}else{
var in_array = false;
for(let i in array1){
// this seems to be the problem area---------------------------------------------------------
if(array1[i][0] === array2[x][0]){
array1[i] = array2[x];
in_array = true;
}
}
if(!in_array){
array1.push(array2[x]);
}
}
}
return array1;
}
error log
So I'm currently making a discord bot so whenever someone types !true #USER [Location] it will DM the #USER a message, add a role and then nickname the user to [Location] with the following code :
const mention = message.mentions.members.first();
msg = message.content.toLowerCase();
if (msg.startsWith(prefix)){
if(message.channel.id === '12345'){
if (msg.startsWith (prefix + "true")){
if(!message.member.hasPermission("MANAGE_NICKNAMES")) return message.reply("You have no permission!");
if (mention == null) { return; }
let args = message.content.split(" ").slice(2);
if ((mention.displayName + " " + args.join(" ")).length > 32) return message.channel.send("The nickname exceeds 32 characters")
if(mention.roles.cache.has('1234567890')){
message.reply("User is already accepted.")
} else {
mention.roles.add('1234567890').then(() => mention.setNickname(mention.displayName+" "+args.join(' ')))
.catch((e) => {
console.log('handle error here: ', e.message)
})
}
}}}
However, most of the time it will return with Cannot read property 'first' of null and it won't change the user's nickname (only roles and DM). Is there anything wrong with my code? Thanks.
const mention = message.mentions.members.first();
In the above code, message.mentions.members is null so you can't access a property (here first) in a null object.
The culprit is probably somewhere you set members property, not the code you provided here. do some debugging and console.log and you'll fix the issue.
You got error because you can`t get first of null. Better check mention in a command, like this.
if (msg.startsWith(prefix)) {
if (message.channel.id === '12345') {
if (msg.startsWith(prefix + 'true')) {
if (args.length < 0) return message.reply('PLS mention a member or write his ID');
const mention = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
if (!mention) return message.reply('Can`t find a member');
msg = message.content.toLowerCase();
if (!message.member.hasPermission('MANAGE_NICKNAMES')) return message.reply('You have no permission!');
if (mention == null) {
return;
}
let args = message.content.split(' ').slice(2);
if ((mention.displayName + ' ' + args.join(' ')).length > 32) return message.channel.send('The nickname exceeds 32 characters');
if (mention.roles.cache.has('1234567890')) {
message.reply('User is already accepted.');
} else {
mention.roles
.add('1234567890')
.then(() => mention.setNickname(mention.displayName + ' ' + args.join(' ')))
.catch(e => {
console.log('handle error here: ', e.message);
});
}
}
}
}
I have just created a simple bot with that code:
client.on('message', async message => {
if (!message.author.bot) {
if (!getClub(message) && (message.channel.id == channel_bot || message.channel.type != "dm")) { //
getRadio(message);
}
} else if (message.channel.id = channel_dev && message.author.bot) {
getDevCommands(message);
}
});
and I check bot command with
function getClub(msg) {
const args = msg.content.slice(msg.content.includes(config.prefix) ? config.prefix.length : 0).trim().split(/ +/g);
let isClub = false;
club_commands.forEach(function (element) {
if (element.id == "club" && element.commands.includes(args[0])) {
isClub = true;
}
});
if (!isClub) {
return false;
}
club_commands.forEach(function (element) {
// element is parsed object from JSON: {"id":"join", "commands":"join,attach,invite..."}
if (element.commands.includes(args[1])) {
switch (element.id) {
case "stats":
clubStats(msg);
return true;
case "join":
clubParticipation(msg, 1);
return true;
case "leave":
clubParticipation(msg, 0);
return true;
default:
// do nothing
break;
}
}
});
return false;
}
So in clubPartisipation() im getting in msg.channel.id - actual channel id but only "true" for the all next messages
function clubParticipation(msg, status) {
const args = msg.content.trim().split(/ +/g).splice(2, 2).join("");
if (args.length <= 3) {
msg.channel.send("test0");
} else {
let member = guild.members.get(msg.author.id);
if (status == "1") {
msg.channel.send("test1").catch(console.log);
} else {
msg.channel.send("test3").catch(console.log);
}
getHTTPResponce(config.server_url + 'add/club/participation?channel_id=' + msg.channel.id + '&status=' + status + '&user_id=' + member.id + '&club_id=' + Base64.encode(Base64.encode(args)) + '&token=' + config.server_token, msg)
.catch(console.log);
}
}
Error code is
{ DiscordAPIError: Invalid Form Body
channel_id: Value "true" is not snowflake.
at item.request.gen.end (/root/curatortojps/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15
)
at then (/root/curatortojps/node_modules/snekfetch/src/index.js:215:21)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
name: 'DiscordAPIError',
message: 'Invalid Form Body\nchannel_id: Value "true" is not snowflake.',
path: '/api/v7/channels/true/messages',
code: 50035,
method: 'POST' }
In your first block of code, you have:
(message.channel.id = channel_dev && message.author.bot)
= is an assignment operator. This means that you're setting message.channel.id to the value of channel_dev && message.author.bot, a boolean (true or false).
You should use an equality operator like == or === to compare the value of message.channel.id. For the difference between the two, check out this answer.
(message.channel.id === channel_dev && message.author.bot)
I have some functions that each one uses an object as an argument.
All these objects have similar structure, so instead of making the same checks in each function, I've created some check functions:
const checkIfOptionsIsObject = options => {
if (typeof options === 'object') {
return true;
} else {
throw new TypeError('The function argument should be an object.');
}
}
and
const checkOptionsMinMax = options => {
if (isNaN(options.min) || isNaN(options.max)) {
throw new TypeError("'min' and 'max' should both be numbers.");
} else if (options.min > options.max) {
throw new Error("'min' shouldn't be greater than 'max'");
} else {
return true;
}
}
And here is how I am using them:
const firstFunction = options => {
checkIfOptionsIsObject(options);
checkOptionsMinMax(options);
// do some stuff
return result;
}
const secondFunction = options => {
checkIfOptionsIsObject(options);
checkOptionsMinMax(options);
// do some other stuff
return result;
}
const thirdFunction = options => {
checkIfOptionsIsObject(options);
// do some stuff that doesn't use min and max
return result;
}
Is there any problem in this code?
1) note that in your first check typeof object === 'object' if that variable named object is actualy an array it will also give you the type of 'object'. You can see this in your console by entering typeof [ ] === 'object' and notice that it'll return true. It's better to use o !== null && typeof o === 'object' && Array.isArray(o) === false; for object testing.
const checkIfOptionsIsObject = options => {
if (options !== null && typeof options === 'object' && Array.isArray(options) === false) {
return true;
} else {
throw new TypeError('The function argument should be an object.');
}
}
I send some http requests and after the Promises have solved I need to loop through the array and to do some async things. I want to leave the function asynchronous but I need to execute setState only after "map" loop is finished:
fetchData = () => {
let data = [];
let tickets = http.get('/api/zendesk/tickets'),
users = http.get('/api/zendesk/users'),
items = http.get('/api/orders/items'),
reqTypes = http.get("/api/support/requests");
Promise.all([users,tickets,items,reqTypes])
.then(values => {
console.log(values);
let usersArr = values[0].data.users,
ticketsArr = values[1].data.tickets,
itemsArr = values[2].data,
requestArr = values[3].data;
data = ticketsArr.map((ticket,key) => {
let id = ticket.custom_fields.find(field => field.id === productions_fields.order_id || field.id === develop_fields.order_id).value;
id === null ? ticket.Items = [] : ticket.Items = itemsArr.filter(item => item.order_id === parseInt(id));
ticket.Requests = requestArr.filter(request => request.id === ticket.id);
})
this.setState({tickets:data})
}).catch(err => console.log(err))
}
Is it possible to do or I need to make sync loop like "for"?
I get an array of "undefined objects", Why?
Because you are not returning anything inside map body, and by default it returns undefined.
Check this snippet:
let a = [1,2,3,4].map(el => {
console.log('el = ', el);
})
console.log('a = ', a); //array of undefined values
If you wants to modify the existing array, then use forEach instead of map.
Like this:
ticketsArr.forEach((ticket,key) => {
let id = ticket.custom_fields.find(field => field.id === productions_fields.order_id || field.id === develop_fields.order_id).value;
ticket.Items = (id === null) ? [] : itemsArr.filter(item => item.order_id === parseInt(id));
ticket.Requests = requestArr.filter(request => request.id === ticket.id);
})
this.setState({ tickets: ticketsArr })
Array.prototype.map expects you to return how each element should be in its final form. Right now you are not returning anything, hence every element becoming undefined.
data = ticketsArr.map((ticket,key) => {
let id = ticket.custom_fields.find(field => field.id === productions_fields.order_id || field.id === develop_fields.order_id).value;
id === null ? ticket.Items = [] : ticket.Items = itemsArr.filter(item => item.order_id === parseInt(id));
ticket.Requests = requestArr.filter(request => request.id === ticket.id);
return ticket // <- this
})