Asana addProject trouble in node - node.js

I'm trying to create a task and then set a project on the task using nodejs and the asana thin wrapper available from npm.
var asana = require('asana');
var newTask = { name: "Your Mission", notes: "Stuff" };
var project = [{ id:321, name: "Missions Impossible"}];
var client = asana.Client.basicAuth('APIKEY');
client.tasks.createInWorkspace(123, newTask).then(function(task) {
client.tasks.addProject(task.id, project).then(function(o) {
// Check for empty object returned (sign of success)
if (Object.keys(o).length === 0)
console.log('yay!');
else
console.log('booo');
}
The task is created, but I get an error in the addProject method - "Possibly unhandled error. Invalid Request".
I've tried different variations on the project object, but I'm out of ideas.
Is the project wrongly formed? Something else?

You are right, your project is malformed. The data passed to the addProject method should be a dictionary with the member:
project: 321
or
project: { id: 321 }
See the documentation for the endpoint being called and the data that it gets passed.

Related

Why is AxelSpringer Google PubSub library causing the error "Unable to detect a Project Id in the current environment"?

I am implementing GraphQL subscription based on this document using the PubSub Class. However,
the document also says:- The PubSub class is not recommended for
production environments, because it's an in-memory event system that
only supports a single server instance. After you get subscriptions
working in development, we strongly recommend switching it out for a
different subclass of the abstract PubSubEngine class. Recommended
subclasses are listed in Production PubSub libraries.
So, I chose the Google PubSub Library, the second option from the list.
In a file subscription.js, I wrote the following code:-
const { GooglePubSub } = require('#axelspringer/graphql-google-pubsub');
const pubsub = new GooglePubSub();
const UPDATE_PARKING = "UPDATE_PARKING";
module.exports = {pubsub, UPDATE_PARKING};
In the file named parking.queries.js, the Graphql is like this:-
type Subscription {
updateParking: Parking
}
In the file named parking.resolver.js, the Resolver is like this
const {pubsub, UPDATE_PARKING} = require("../utils/subscription");
Subscription: {
updateParking: {
subscribe: () => pubsub.asyncIterator(UPDATE_PARKING)
}
}
Now to publish the data in the service file named parking.service.js, I did this:-
const {pubsub, UPDATE_PARKING} = require("../utils/subscription");
.........................
.........................
.........................
pubsub.publish(UPDATE_PARKING, {updateParking: parkingDetail});
.........................
The above code runs fine. However, when I an trying to publish a new subscription, I am facing a strange problem. Let, me explain.
In the file subscription.js, I made the following changes:-
const { GooglePubSub } = require('#axelspringer/graphql-google-pubsub');
const pubsub = new GooglePubSub();
const UPDATE_PARKING = "UPDATE_PARKING";
const UPDATE_USER = "UPDATE_USER";
module.exports = {pubsub, UPDATE_PARKING, UPDATE_USER};
In the file named user.queries.js, the Graphql is like this:-
type Subscription {
updateUser: User
}
In the file named user.resolver.js, the Resolver is like this,
const {pubsub, UPDATE_USER} = require("../utils/subscription");
Subscription: {
updateUser: {
subscribe: () => pubsub.asyncIterator(UPDATE_USER)
}
}
In the service file user_subscription.service.js, I have the code like this:-
const {pubsub, UPDATE_USER} = require("../utils/subscription");
............................
............................
pubsub.publish(UPDATE_USER, {updateUser: newUserData});
............................
Whenever the code pubsub.publish(UPDATE_USER, {updateUser: newUserData}); is getting executed, I am getting this strange error:-
/var/www/example/html/node_modules/google-auth-library/build/src/auth/googleauth.js:89
throw new Error('Unable to detect a Project Id in the current environment. \n' +
^
Error: Unable to detect a Project Id in the current environment.
To learn more about authentication and Google APIs, visit:
https://cloud.google.com/docs/authentication/getting-started
at /var/www/example/html/node_modules/google-auth-library/build/src/auth/googleauth.js:89:31
at processTicksAndRejections (node:internal/process/task_queues:96:5)
[nodemon] app crashed - waiting for file changes before starting...
However, whenever this code is executed pubsub.publish(UPDATE_PARKING, {updateParking: parkingDetail}); the above error doesn't happen.
What am I doing wrong?

always failed to create a document when it has a Firestore Geopoint field when using Firebase rules unit testing library

I am tryin to perform testing to my security rules using #firebase/rules-unit-testing library using emulators
I have an Event class like this
class Event {
title: string
coordinate: FirebaseFirestore.GeoPoint
}
create an instance
const event = new Event("event title", new admin.firestore.GeoPoint(-6.931980, 107.559540))
and then I write testing for my security rules like this using Mocha
import * as firebase from "#firebase/rules-unit-testing";
function getFirestore(auth?: TokenOptions) {
return firebase.initializeTestApp({projectId: projectID, auth: auth}).firestore();
}
it("should create an event document", async () => {
const db = getFirestore(myAuth);
const ref = db.collection("events").doc("myEvent");
const event = new Event("event title", new admin.firestore.GeoPoint(-6.931980, 107.559540))
const eventData = { ...event};
const promise = ref.set(eventData);
await firebase.assertSucceeds(promise);
});
but I have error
FirebaseError: Function DocumentReference.set() called with invalid
data. Unsupported field value: a custom object (found in field
coordinate in document events/myEvent)
the error said that the coordinate property using custom object (i.e FirebaseFirestore.GeoPoint)
but I need to test my security rules. I have rules like this
request.resource.data.coordinate is latlng
so how do I test if a field is latlng or not but, I can't create a document which is a Firestore Geopoint?
is it a bug?
I have found the answer from thebrianchen in here
The issue here is that the web and admin SDKs have separate GeoPoint
classes that are not interoperable with each other. If you're using
the web SDK with initializeTestApp(), you'll have to the GeoPoint
class from the web SDK. In your case, you'll need to use the web SDK's
GeoPoint class:
so I have to change the way I create GeoPoint data. From
new admin.firestore.GeoPoint(-6.931980, 107.559540)
to be
import * as firebase from "#firebase/rules-unit-testing";
new firebase.firestore.GeoPoint(-6.931980, 107.559540)

getting a parse error and cannot find eslint file

I've realized that you can't send messages directly from the client with the FCM v1 API so now I'm using node.js and I wan't to deploy this cloud function, but I am getting a parse error like so:
This is my function:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { event } = require("firebase-functions/lib/providers/analytics");
admin.initializeApp(functions.config().firebase);
exports.sendNotificationsToTopic =
functions.firestore.document("school_users/{uid}/events/{docID}").onWrite(async (event) => {
let docID = event.after.id;
let schoolID = event.after.get("school_id")
let title = "New Event!!"
let notificationBody = "A new event has been added to the dashboard!!"
var message = {
notification: {
title: title,
body: notificationBody,
},
topic: schoolID,
};
let response = await admin.messaging().sendToTopic(message);
console.log(response);
});
I did some research and found people were getting similar errors with their projects, and answers were saying to update the version of eslint so it picks up on the shorthand syntax, I can't figure out where to find the eslint file to update the version. Does anybody know where I can find this file?
All I needed to do was show my hidden files in my functions directory. Once I seen the .eslintrc.js file, I simply removed a part of a value from the "eslint" key, and the function deployed perfectly fine.

How to setup Google Translate API for Node.js?

I want to use Googles Cloud Translation API in my Node.js application, however I'm getting a The request is missing a valid API key. error.
I have followed the Quickstart guide provided by Google.
I have created GCP project, downloaded the private key as JSON file and setup the environment variable in Powershell (img).
After that I've installed the library with
yarn add #google-cloud/translate
The code I'm running in my translate.js file comes from the Quickstart guide with additional try-catch blocks:
async function quickstart(
projectId = process.env.PROJECT_ID // Project Id from JSON file
) {
try {
// Imports the Google Cloud client library
const { Translate } = require('#google-cloud/translate');
// Instantiates a client
const translate = new Translate({ projectId });
// The text to translate
const text = 'Hello, world!';
// The target language
const target = 'ru';
// Translates some text into Russian
const [translation] = await translate.translate(text, target);
console.log('Text:', text);
console.log('Translation:', translation);
} catch (error) {
console.error(error);
}
}
quickstart();
When I then run node translate.js, I'll get an Error:
{ Error: The request is missing a valid API key.
...
code: 403,
errors:
[ { message: 'The request is missing a valid API key.',
domain: 'global',
reason: 'forbidden' } ],
response: undefined,
message: 'The request is missing a valid API key.' }
I am on Windows 10, Node v10.13.0.
Believe you would have missed this environment variable defining , before starting the node service
Replace [PATH] with the file path of the JSON file that contains your service account key, and [FILE_NAME] with the filename.
With PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
For example:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"

get json data from realtime database to Dialogflow inline editor (google assistant)

Beginner here on programming, I'm working on my free time on a project which is related to Google Assistant, for the first time i'm using the Firebase Realtime Database and don't know how to get the data from there, the code below is in Dialogflow's inline editor, where is category1 2 and 3 inside those are students with credits. I have made some changes and put these three (categories1 2 and 3) in the database picture below, i want to remove these three categories from code and replace it with the ones in realtime database.
Because this is my first time using that database, I don't know how to get that data from there with nodejs.
function categoryac(agent) {
const categories = {
category1: {"Alex" : 25, "Jennifer" : 45, "Justin" : 35, "Peter" : 89},
category2: {"Alex" : 95, "Jennifer" : 75, "Justin" : 85, "Peter" : 59},
category3: {"Alex" : 67, "Jennifer" : 55, "Justin" : 45, "Peter" : 15},
};
const categoried = agent.parameters["categoried"];
const categoryAmount = agent.parameters["categoryAmount"];
const category = category[categoried];
const aggregate = category[categoryAmount];
agent.add(`${categoried}'s ${categoryAmount} is ${aggregate}`);
}
let intentMap = new Map();
intentMap.set('category', categoryac);
agent.handleRequest(intentMap);
});
UPDATE
I used the code below, like this:
function categoryac( agent ){
const categoried = agent.parameters["categoried"];
const categoryAmount = agent.parameters["categoryAmount"];
var admin = require( 'firebase-admin' );
admin.initializeApp( {
credential: admin.credential.cert( {
projectId: ' ',
clientEmail: ' ',
privateKey: ' '
} ),
dblink: 'www.*****.*****.com'
} );
var thing = admin.database();
var relating= thing.ref( `category/${categoried}/${categoryAmount}` );
return relating.once( "value" ).then( snapshot =>{
var aggregate = snapshot.value();
agent.add( `${categoried}'s ${categoryAmount} is ${aggregate}` );
} )
.catch( fail =>{
agent.add( 'uh oh, something went wrong.' );
console.error( fail );
} );
}
let intentMap = new Map();
intentMap.set( 'category', categoryac );
agent.handleRequest( intentMap );
} );
got error message : 'MalformedResponse
Failed to parse Dialogflow response into AppResponse because of empty speech response.' error from log :
{
insertId: "v*****2"
labels: {
channel: "preview"
querystream: "GOOGLE_USER"
source: "JSON_RESPONSE_VALIDATION"
}
logName: "projects/****/logs/actions.googleapis.com%2Factions"
receiveTimestamp: "2019-01-07T14:45:29.274840871Z"
resource: {
labels: {
action_id: "actions.intent.TEXT"
project_id: "******"
version_id: ""
}
type: "assistant_action"
}
severity: "ERROR"
textPayload: "MalformedResponse: Failed to parse Dialogflow response into AppResponse because of empty speech response"
timestamp: "2019-01-07T14:45:29.266062732Z"
trace: "projects/383182941858/traces/ABwppHFK_PehMj1XEs_Arng9VL7_zShy-EWvoziK0Ro6v74TaduNG1cJaRMnGAZMoLZhtILdG2hEBkDvJQ"
}
Here is the error in the logs:
Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.
at FirebaseAppError.Error (native)
at FirebaseAppError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseAppError (/user_code/node_modules/firebase-admin/lib/utils/error.js:119:28)
at FirebaseNamespaceInternals.initializeApp (/user_code/node_modules/firebase-admin/lib/firebase-namespace.js:68:23)
at FirebaseNamespace.initializeApp (/user_code/node_modules/firebase-admin/lib/firebase-namespace.js:362:30)
at categoryac (/user_code/index.js:34:11)
at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)
at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:91:9)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
There are several things that you'll need to learn.
The first is how to add the Firebase Admin SDK to your project.
You'll also need to learn how to retrieve the data using the library. Firebase uses a reference-path based method to fetch the data, so you need to make sure you build the path correctly.
Finally, since you're doing this inside a fulfillment handler, and you are making an asynchronous call, you need to make sure you return a Promise. Fortunately, fetching the data also involves returning a Promise, so you can return this Promise.
The code might, partially, look something like this (untested):
function personFacts(agent) {
const personId = agent.parameters["personId"];
const personMeasurement = agent.parameters["personMeasurement"];
var db = admin.database();
var ref = db.ref(`person/${personId}/${personMeasurement}`);
return ref.once("value")
.then( snapshot => {
var result = snapshot.val();
agent.add(`${personId}'s ${personMeasurement} is ${result}`);
})
.catch( err => {
agent.add('uh oh, something went wrong.');
console.error( err );
});
}
As you noted, you need to initialize the Firebase admin library with a key that will give you access through the service account. You can generate the key and download it, and then point to the folder where you have saved it. (It looks like you've just inlined the information, which also works.)
The "Malformed Response" error means that no response was set. This can be due to a number of things, but generally means that your program crashed or failed to call agent.add() for some reason. Consult the logs of your action running for more info. (If you are using the Dialogflow Inline Editor, you can get to the logs by going to https://console.firebase.google.com/, selecting your project, selecting the "Functions" tab on the left, and selecting the "logs" tab.)
Update based on the code and error message.
As the error message suggests, you called admin.initializeApp() more than once. This should only be done when the function is first configured, rather than each time your function is called. Once initialized once - it can be used multiple times.
In your case, this can be done by moving the require that imports firebase-admin and the call to admin.initializeApp() out of the personFacts() function and put them both closer to the top - probably right after the other require() calls.

Resources