googleapis throwing 'undefined' error in apirequest.js - node.js

I have a problem with the NodeJs googleapis module.
My Simple Test:
var google = require('googleapis');
var analytics = new google.analytics('v3');
var OAuth2 = google.auth.OAuth2;
var config = require('./config.js')
var oauth2Client = new OAuth2(config.clientId, config.clientSecret);
// Retrieve tokens via token exchange explained above or set them:
oauth2Client.setCredentials({
access_token: config.accessToken,
refresh_token: config.refreshToken
});
// get management list
analytics.management.accounts.list({ auth: oauth2Client }, function(err, response) {
console.log(err)
console.log(response)
});
My Result:
Type Error: Cannot read property 'params' of undefined
at createAPIRequest (/Users/gkburdett/Documents/Applications/maestro-and-scout/node_modules/googleapis/lib/apirequest.js:76:39)
at Object.Analytics.management.accounts.list (/Users/gkburdett/Documents/Applications/maestro-and-scout/node_modules/googleapis/apis/analytics/v3.js:342:16)
at Object.<anonymous> (/Users/gkburdett/Documents/Applications/maestro-and-scout/server/google/test/simpleTest.js:13:31)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
parameters.context.google is a blank object.
It works if I change apirequest.js line 76 to a blank object. I submitted an issue on GitHub, but I have a hard time believing I'm the only one having this problem if it's a problem with the library.

Change line 2 from:
var analytics = new google.analytics('v3');
to:
var analytics = google.analytics('v3');
When calling analytics.management.accounts.list the context options being passed to createAPIRequest are out of scope to the initial google object.

There was a method in my api with a name that started with "google" and it was overriding some internal config object of the library. I am not a guru to judge anyone but it seems like a poor design to mix internal and user-defined methods in one object

Related

Client Missing Intents

I have decided I wanted to make my own Discord bot so I started watching a tutorial on Youtube by #CodeLyon. I have followed the procedures exactly and this is my code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Vibre is online!');
});
Of course, I have not included the Client Login code but when I try to "run" this program in command prompt with "node .", this is what happens:
C:\Users\Isaac\Desktop\DiscordBot>node .
C:\Users\Isaac\Desktop\DiscordBot\node_modules\discord.js\src\client\Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions
(C:\Users\Isaac\Desktop\DiscordBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:544:13)
at new Client
(C:\Users\Isaac\Desktop\DiscordBot\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:73:10)
at Object.<anonymous> (C:\Users\Isaac\Desktop\DiscordBot\main.js:3:16)
←[90m at Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain]
(internal/modules/run_main.js:76:12)←[39m
←[90m at internal/main/run_main_module.js:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m
}
Can somebody please help?
In CodeLyon's video he used discord.js#12.2.0 but now discord.js updated to 13.1.0 and with this updated you must send ClientOptions as a parameter while creating new client. So now you need to specify the events which you want your bot to receive using gateway intents.
Instead of
const client = new Discord.Client();
Use
const client = new Discord.Client({intents: [Enter intents you need here]});
Example
const client = new Discord.Client({intents: ["GUILD", "GUILD_MESSAGES"]})

How do I make an API call inside a Firebase Cloud Function?

I'm trying to create a Firebase Cloud Function where I make an API call to NewsAPI and retrieve a set of articles. The code below is an example, and it aims to retrieve articles from BBCNews.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var url = 'http://newsapi.org/v2/top-headlines?' +
'sources=bbc-news&' +
'apiKey={myKey}';
const GetArticles = () => (
fetch(new Request(url))
.then(function (response) {
console.log(response.json());
})
);
When I run this code I get the following error message:
ReferenceError: Request is not defined
at Object.<anonymous> (C:\Kesav\Coding stuff\project-academy\functions\index.js:9:11)
at Module._compile (internal/modules/cjs/loader.js:1185:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
at Module.load (internal/modules/cjs/loader.js:1034:32)
at Function.Module._load (internal/modules/cjs/loader.js:923:14)
at Module.require (internal/modules/cjs/loader.js:1074:19)
at require (internal/modules/cjs/helpers.js:72:18)
at C:\Users\kkosa\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:661:33
at Generator.next (<anonymous>)
at fulfilled (C:\Users\kkosa\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:5:58)
How would I fix this?
You apparen't don't have any code that imports Request. That's not a "free object" in nodejs. You're going to have to find an HTTP client library for node (you have many options), import it, and use that in your code.

I just followed the step by step process on google cloud function tutorial , anyone know how to fix this? or the cause of the problem?

I only followed the step by step tutorial on google firebase
this is the link:
https://firebase.google.com/docs/functions/get-started?authuser=0
and then I get these on cmd
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:9
exports.addMessage = functions.https.onRequest(async(req, res) => {
^
SyntaxError: Unexpected token (
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at getUserFunction (/var/tmp/worker/worker.js:439:24)
Functions deploy had errors with the following functions:
addMessage
These are the codes on the tutorial.
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async(req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.`enter code here`
const snapshot = await admin.database().ref('/messages').push({original:
original});
// Redirect with 303 SEE OTHER to the URL of the pushed object in the
Firebase console.
res.redirect(303, snapshot.ref.toString());
});
Looking at the code you posted compared to the code in the example, I think the problem is:
Your code:
exports.addMessage = functions.https.onRequest(async(req, res) => {
example code:
exports.addMessage = functions.https.onRequest(async (req, res) => {
Notice that in your code there is no "space" character after async. Async is a qualifier of a function that flags the function as asynchronous. See here for details of async.
The omission of the space changes the semantics. If I had to guess, the runtime would now be looking for a function that is called async. Please change your code to include a space.

Sending email by using maildrill

I am trying to send email through maildrillapp but unable to send.
I install related packages and take this code from Here.
My Code for app.js is:
var nodemailer = require("nodemailer");
var mandrillTransport = require('nodemailer-mandrill-transport');
/*
* Configuring mandrill transport.
* Copy your API key here.
*/
var smtpTransport = nodemailer.createTransport(mandrillTransport({
auth: {
apiKey : 'xxxxxxxxxxxxxxxxxxx'
}
}));
// Put in email details.
let mailOptions={
from : 'example#domain.com',
to : 'example#domain.com',
subject : "This is from Mandrill",
html : "Hello,<br>Sending this email using Node and Mandrill"
};
// Sending email.
smtpTransport.sendMail(mailData,function(error, response){
if(error) {
throw new Error("Error in sending email");
}
console.log("Message sent: " + JSON.stringify(response));
});
when I run this it throws following error
'C:\office\new 3\maildrill\app.js:23
smtpTransport.sendMail(mailData,function(error, response){
^
ReferenceError: mailData is not defined
at Object. (C:\office\new 3\maildrill\app.js:23:24)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3 PS C:\office\new 3\maildrill>'
Here is the some demo for sending emails with nodemailer nodemailer
I know this is old, but I found the same issue using the same example I found online so am posting an answer for general reference.
The problem is that the author defines 'mailOptions' but references it as 'mailData' instead. I'm guessing they decided to rename the variable but only did half the job!
Update the sendMail command accordingly and it'll work.
smtpTransport.sendMail(mailOptions,function(error, response){

throw new Error('Missing PFX or certificate + private key')

I have previously developed a module in node.js and after complete testing put it in git hub. Now i downloaded the zipped version of same module from githup and tried to run the module, installed all the dependencies, But now i am getting the following error
Error:Missing PFX + certificate + private key
The complete Error Log is as follows:
Error: Missing PFX or certificate + private key.
at HTTPSServer.Server (tls.js:1029:11)
at HTTPSServer.Server (https.js:35:14)
at HTTPSServer (C:/Social/node_modules/express/node_modules/connect/lib/https.js:34:16)
at new HTTPSServer (C:/Social/node_modules/express/lib/https.js:38:23)
at Object.exports.createServer (C:/Social/node_modules/express/lib/express.js:43:12)
at Object.<anonymous> (C:/Social/app.js:46:36)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
I tried to find the solution but could not find any. Can anyone help me to with the same. Thanks in advance.
For some reason, Express thinks you want to start an HTTPS server. My guess would be this is because of this line in your code:
var app = module.exports = express.createServer(form({ keepExtensions: true }));
(link)
However, Express uses this code to see if it should start an HTTPS server:
exports.createServer = function(options){
if ('object' == typeof options) {
return new HTTPSServer(options, Array.prototype.slice.call(arguments, 1));
} else {
return new HTTPServer(Array.prototype.slice.call(arguments));
}
};
Which is a bit strange, since form() returns a function and not an object. But to be sure, try rewriting your code to this:
var app = module.exports = express.createServer();
app.use(form({ keepExtensions: true }));

Resources