There seems to be an issue with Google Cloud Function - res.setHeader function is not recognized

I attempted to run the initial example from the Cloud Functions Documentation. I went ahead and created my function, then simply copied and pasted the example code provided in the docs.

Correction: Instead of using the original example, I took the code snippet from:

The first error message I encountered was;

res.setHeader is not a function

After that, I attempted to use a different function to set the header like so;

res.writeHead(200, { 'Content-Type': 'application/json' });

Unfortunately, this approach also resulted in the same error. Here is the chunk of code in question;

/**
 * Cloud Function.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.helloHttp = function helloHttp(req, res) {
    response = "This is a sample response from your webhook!" //Default response from the webhook to show it's working

    // res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type
    res.writeHead(200, { 'Content-Type': 'application/json' });
    //"speech" is the spoken version of the response, "displayText" is the visual version
    res.send(JSON.stringify({
        "speech": response,
        "displayText": response        
    }));
};

Here is how it appears on the test console;

https://i.stack.imgur.com/a9mAr.png

Do you have any insights into what might be missing or causing these errors?

Answer №1

My error was choosing the wrong trigger during creation, I needed to select the HTTP trigger instead. Everything is functioning properly now.

https://i.stack.imgur.com/xYznp.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Execute test scenarios and upon successful completion, initiate deployment of a React JS application using Jenkins

I am currently facing an issue with running test cases and deploying a React-js app using Jenkins. After pushing my code to Git, I can successfully run the React-js app locally. However, when trying to execute the second command mocha (which is used to ru ...

What steps can you take to prevent a potential crash from occurring when an unauthorized user attempts to access a page without logging in?

I've encountered an issue where the app crashes when trying to access a page that requires logging in. The reason for this crash is because the page attempts to load undefined data, resulting in the error: TypeError: Cannot read property 'firstN ...

Issues are arising with the for loop in an express node js app using ejs, as it is not displaying the intended data and

I am currently utilizing a for loop in JavaScript to display all the users from the database using ejs. I have included the code snippet below. This is within an express/node js application where SQL is used for data storage. <div class = "Contacts ...

Error encountered when attempting to establish a connection between socket.io and express: network error

Similar Question: socket.io: Failed to load resource I'm having trouble getting a simple express + socket.io hello world app to work. I keep receiving the following error: "NetworkError: 404 Not Found - http:// localhost:3002/socket.io/socke ...

Using Gmail in conjunction with Heroku for email delivery

After completing an order in my web app, I want to automatically send a confirmation email. I decided to use Nodemailer as it is a popular npm package for this purpose. I successfully coded the functionality and tested it in my local environment. Howeve ...

Utilizing postBack to send chatbot responses with BotFramework v4 and Node.js

I'm looking to send a postBack text message to my bot, but I need help with the correct syntax. Here's the code snippet: if (postback.payload == "WHAT_IS_MENTAL_HEALTH") { await turnContext.sendActivity("TO-DO: Forward on 'What Is Me ...

Having trouble transmitting data with axios between React frontend and Node.js backend

My current challenge involves using axios to communicate with the back-end. The code structure seems to be causing an issue because when I attempt to access req.body in the back-end, it returns undefined. Here is a snippet of my front-end code: const respo ...

Tips for incorporating external routes into the routes index file

I am currently dealing with a users.js and an index.js file. users.js const express = require('express'); const router = express.Router(); const {catchErrors} = require('../handlers/errorHandlers'); const authController = require(&ap ...

Retrieve all records in which a Sequelize association uses hasOne

Currently, I am exploring the `sequalize association - hasOne` feature. However, I encountered an unexpected issue where all the rows from the `USER` table are being returned instead of just the rows from the `system_admin` table. Is this the intended beha ...

Tips on Implementing Variables in Jade File Post-Rendering

After logging in, I wanted to display the user's name as a link in my index.jade file, but unfortunately nothing is showing up. I've experimented with both req.body.username and req.session, but neither of them seem to be working correctly. Here ...

Is there a way to run a node script from any location in the command line similar to how Angular's "

Currently, I am developing a node module that performs certain functions. I want to create a command similar to Angular's ng command. However, I am facing compatibility issues with Windows and Linux operating systems. Despite my attempts to modify the ...

Tips for executing an asynchronous function in a synchronized manner within a Node.js environment

Currently, I am utilizing the Express framework. The issue that I am encountering involves a variable called 'duration'. Whenever a request is made to '/', the total duration of a given video is calculated and saved to this variable. H ...

Wiki experiencing issues with NodeJS HttpGet functionality

Goal Retrieve the HTML content of a Wiki Page. Introduction In an attempt to fetch the HTML of a Wiki page () for data parsing purposes, I am utilizing NodeJS and its HTTP Request methods. Code Snippet Below is the simple code snippet that accesses th ...

Removing information from a MongoDB database with the help of AngularJS and Node.js

Having trouble deleting data in MongoDB using AngularJS and Node.js, as I keep encountering the error "Cannot DELETE /api/manage-product" in the console. .html file <tbody> <tr ng-repeat="product in vm.result"> ...

Is there an rxjs operator that includes both on-next and on-error callbacks?

Is there a similar function to promise.then(onNextCallback,onErrorCallback) in rxjs that I can use? I've already tried alternatives like pipe(concatMap(),catchError) but they are not what I am looking for. ...

Heroku deployment hiccup

image description goes here at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=thawing-harbor-62063.herokuapp.com request_id=f660858a-37c6-4a77-8940-cd7ca253bb6c fwd="76.164.216.25" dyno= connect= service= status=503 bytes= protocol=h ...

UnresolvedPromiseRejectionNotice: MongoDBServerSelectionError: The connection to the 'ip-address' monitor has been terminated. Surprisingly, the code ran smoothly on my colleague's computer

Completely new to this world (less than a month new), my tech lead shared a GitHub repo of an API that I cloned onto my local machine. Following the instructions, I used npm run start in the terminal and received this output: C:\Users\my_path > ...

Getting an ETIMEDOUT error while trying to install a package using npm or yarn

In my Arch Linux system, I recently cleared out the npm cache along with other caches. Now, whenever I try to run npm ping or install something using npm or yarn, I encounter the following error: npm notice PING http://registry.npmjs.org/ npm ERR! code ET ...

Creating a new dynamic page can be achieved by clicking on a dynamically generated link. Would you like to learn how to do that?

Recently, I developed a custom API using Node.js to retrieve information about blogs from Medium.com. The API currently provides: The author/main picture of the article Title A link to the article on medium.com (redundant) The entire article text in the ...

Does the Karma Tast Runner function on node js version 0.12.0?

I'm experiencing an issue where I have Node.js v0.12.0 installed with Karma on my OS X Yosemite, but when I try to run my test task using Gulp, it just hangs as shown in the picture. It seems like PhantomJS is not starting. Interestingly, the same cod ...