google.docs is not a function - node.js

I want to use Google's Docs API but am running into an issue. I followed https://developers.google.com/docs/api/quickstart/nodejs but am having trouble setting it up.
My index.js looks like this:
const functions = require('firebase-functions');
const {google} = require('googleapis');
const {WebhookClient} = require('dialogflow-fulfillment');
const {SimpleResponse, BasicCard, SignIn, Image} = require('actions-on-google');
const calendar = google.calendar('v3');
const people = google.people('v1');
const drive = google.drive('v3');
const docs = google.docs('v1');
process.env.DEBUG = 'dialogflow:*'; // Enable lib debugging statements
My package.json:
"name": "DialogflowFirebaseWebhook",
"description": "Firebase Webhook dependencies for a Dialogflow agent.",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "6"
},
"scripts": {
"lint": "semistandard --fix \"**/*.js\"",
"start": "firebase deploy --only functions, node index.js",
"deploy": "firebase deploy --only functions"
},
"dependencies": {
"firebase-functions": "^2.0.2",
"firebase-admin": "^5.13.1",
"googleapis": "^27.0.0",
"actions-on-google": "2.2.0",
"dialogflow-fulfillment": "0.6.1",
"dialogflow": "0.6.0",
"client-oauth2": "4.2.5"
}
}
Showing only the relevant parts of my script, since it's pretty long, I get this error when I try to run the actual script.
Detailed stack trace: TypeError: google.docs is not a function
Any ideas what I am doing wrong?

Turning my comment into an answer...
It doesn't look like you have anywhere near the current version39 of google apis installed.
You have version 27.
Please run npm install googleapis#39 --save like the doc says.

Related

Firebase Cloud Function - Parsing error: Unexpected token =>

I have a problem during deploy the functions into Cloud Function. I do believe before I put the const admin = require('firebase-admin') and admin.initializeApp() there will be a problem after I place these two, but before I place the two-line there is no problem occurred.
This is my full code for index.js
const functions = require("firebase-functions");
const express = require('express');
const cors = require('cors');
const admin = require('firebase-admin');
admin.initializeApp();
const app = express();
app.get('/', (req, res) => {
});
app.post("/", async (req, res) => {
const user = req.body;
await admin.firestore().collection("users").add(user);
res.status(201).send();
});
exports.user = functions.https.onRequest(app)
package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
.eslintrc.js
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
quotes: ["error", "double"],
},
};
Try adding the second part to your package.json:
{
"parserOptions": {
"ecmaVersion": 2017
}
}
"parser": "babel-eslint" - add this to your.eslintrc.js and run npm install babel-eslint --save this will resolve the issue.

Slow response in Dialog flow with the warning message

I'm trying to use dialogflow fulfillment with the inline editor. But occasionally I get a slow response, and whenever that slow response happens, I get the following warning in the logs
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail
It's weird, since I haven't required firebase-admin into my code, it's simply there in package.json as a dependency (and cannot be removed)
Here are the only things that are getting required
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
And this is my package.json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0"
}
}
The warning message is only that - a warning. It does not mean there is a problem. It is probably happening during environment initialization when the Cloud Function is cold started - hence the slightly slower start.
If you are not using firebase-admin calls, then you have nothing to worry about.
If you are using firebase-admin, then you just need to be sure that the environment is referencing the project you expect.

DialogFlow in Fulfillment - DeepLink is not defined

I'm trying to use DeepLinks in Fulfillment Inline Editor in DialogFlow but I get error "DeepLink is not defined". The question is how to import DeepLink into project?
more info about deeplink class:
deeplink class
My index.js:
'use strict';
// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');
// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');
// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});
// Handle the Dialogflow intent named 'favorite color'.
// The intent collects a parameter named 'color'.
app.intent('favorite color', (conv, {color}) => {
here I have error "DeepLink is not defined"
conv.ask(new DeepLink({
destination: 'Google',
url: 'http://my.deep.link',
package: 'my.package.name',
reason: 'handle this for you',
}))
})
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
My package.json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~6.0"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.5.7",
"dialogflow": "^0.1.0",
"dialogflow-fulfillment": "^0.4.1"
}
}
Thanks!
I've go it! :)
The line 4 should looks like:
const {dialogflow, DeepLink} = require('actions-on-google');

Using a Firestore Cloud Functions

I have this problem. In my code depends of choosen project name I want to retrieve a document from another collection. I am use
exports.updateUser = functions.firestore
.document('Users/{userId}')
.onUpdate((change, context) => {
const newValue = change.after.data().currentProject;
console.log("Project changed to - " + newValue);
const previousValue = change.before.data().currentProject;
console.log("from - " + previousValue);
// ### FROM HERE DOES NOT WORK ###
const projectId = firestore.collection('Projects')
.where('name', '==', newValue).get()
.then(doc => {
console.log(doc.data().id);
return doc.data().id;
});
});
And I get an error
ReferenceError: firestore is not defined
at exports.updateUser.functions.firestore.document.onUpdate (/user_code/index.js:25:14)
So maybe I need another dependencies ?
My package.json is
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
What I am missing? Please give a hint
You will need to use the Firebase Admin SDK to access Firestore collections that are outside of the scope that your function was triggered on. Typically this will look something like:
admin.firestore().collection('Projects')...

Firebase functions cannot deploy : SyntaxError: Unexpected token function

I am trying to deploy a function to firebase and I get an error during deployment
Error: Functions did not deploy properly.
Could it be linked with the async function ?
Actual behavior
Functions get deployed with errors, the cli shows me the following message:
================ console log ================
> eslint .
✔ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (56.39 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: updating function sendContactEmailOAuth...
⚠ functions[sendContactEmailOAuth]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:13
async function getJwt() {
^^^^^^^^
================ functions index.js file ================
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const { JWT } = require('google-auth-library/build/src/index');
exports.sendContactEmailOAuth = functions.https.onRequest((req, res) => {
const sender_msg = 'just a test'
const email = 'contact#lechorodescharentes.org'
async function getJwt() {
const client = new JWT(
functions.config().service_key.client_email,
null,
functions.config().service_key.private_key,
['https://www.googleapis.com/auth/cloud-platform', 'https://mail.google.com'],
);
await client.authorize();
const url = `https://www.googleapis.com/dns/v1/projects/${functions.config().service_key.project_id}`;
const res = await client.request({ url });
console.log(res.data);
}
getJwt();
/* send email with nodemailer to be inserted here */
});
================ package.json file ================
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.2",
"firebase-tools": "^3.18.4",
"google-auth-library": "^1.4.0",
"nodemailer": "^4.6.4"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
As of September 2019:
Update firebase-admin : npm install --save firebase-admin
Update firebase-functions : npm install --save firebase-functions
Add "engines": { "node": "10" } to your /functions/package.json
...
"dependencies": {
"firebase-admin": "^8.5.0",
"firebase-functions": "^3.2.0"
},
"devDependencies": {
"tslint": "~5.19.0",
"typescript": "~3.6.2"
},
"engines": {
"node": "10"
}
...
As of August 2018:
Cloud Functions now support Node 8 (8.11.1). Check out this blog post.
Upgrade to Node 8
As suggested in this blog post, follow these steps to upgrade to Node 8:
Upgrade your firebase-functions version via npm install --save firebase-functions#latest
Upgrade firebase-tools via npm update -g firebase-tools
Add "engines": { "node": "8" } to your /functions/package.json
If you are still having the issue on a recent version (such as node 12), use the ecmaVersion parser option in your .eslintrc.js file.
Here's a sample:
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
parserOptions: {
ecmaVersion: 8,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
quotes: ["error", "double"],
},
};
h/t to Dean for the original suggestion.

Resources