I would like to write to DB after i redirect a user:
exports.contentServer = functions.https.onRequest((request, response) => {
...
...
return response.redirect(finalUrl + "front?action=" + action )
.then(function(){ // **** error : .then of undefined
....
I get an error in the promise.
Cannot read property 'then' of undefined
at exports.contentServer.functions.https.onRequest
As far as I can tell redirect doesn't return a Promise. In fact, from the undefined in your error message it seems like it doesn't return anything at all. The documentation also doesn't show any return value: https://expressjs.com/en/api.html#res.redirect
If it were to return something, you could capture that value and return it later:
const result = response.redirect(finalUrl + "front?action=" + action )
....
return result;
Related
Can somebody PLEASE tell me what I'm doing wrong here :
I'm sorry, I feel like I'm losing my mind for 30 minutes.
getChartData() {
this.http
.get('http://localhost:3001/transactions/' + sessionStorage.getItem('id'))
.toPromise()
.then((data: any) => {
this.data = data;
});
}
<button (click)="getChartData()">click</button>
Error :
TypeError: undefined is not an object (evaluating 'this.http.get')
You have to return something from your function. With your current code example, If you hover your mouse over the function name, you'll see it says void.
Also, I recommend using observables rather than promises, Angular uses heavily
getChartData() {
// return the HTTP response
return this.http.get(
'http://localhost:3001/transactions/'
+ sessionStorage.getItem('id'))
}
Then simply, subscribe to it to get the response back
someFunction() {
this.service.getChartData().subscribe((response) => console.log(response))
}
I am making a function in nodejs to read a value from the database in Firebase and return it. I read in the that way and get the right value by the console.log(), but when i made the return of the value doesn´t work properly when i call that function
function test() {
database.ref(sessionId + '/name/').once("value").then((snapshot) => {
var name = snapshot.child("respuesta").val()
console.log(name);
return name;
});
}
If someone could help me. Thanks
You're not returning anything from test, so undefined is being implicitly returned. You can return the promise like this:
function test() {
return database.ref(// everything else is identical
}
Note that this will be returning a promise that resolves to the name, not the name itself. It's impossible to return the name, because that doesn't exist yet. To interact with the eventual value of the promise, you can call .then on the promise:
test().then(name => {
// Put your code that uses the name here
})
Or you can put your code in an async function and await the promise:
async function someFunction() {
const name = await test();
// Put your code that uses the name here
}
I'm trying to return a variable via a API, but when I go to return it, it always returns it as undefined
There is the function:
function getCityName()
{
console.log("Trying to return city location...");
var url = util.format('https://api.truckyapp.com/v2/map/%s/resolve?x=%s&y=%s', game.game.name, data.position.X, data.position.Y);
fetch(url).then(function(data) {
return data.json();
}).then(function(parsed){
return parsed.response.poi.realName;
});
}
Code to run the function:
var city = getCityName();
console.log("City name is: "+city)
INFO: The JSON shows correctly
You would need to return the final value returned by the callback from the getCityName function:
function getCityName()
{
console.log("Trying to return city location...");
var url = util.format('https://api.truckyapp.com/v2/map/%s/resolve?x=%s&y=%s', game.game.name, data.position.X, data.position.Y);
return fetch(url).then(function(data) {
return data.json();
}).then(function(parsed){
return parsed.response.poi.realName;
});
}
The above would cause getCityName to return a Promise. To get the actual value of the city, you would now have to wait for the Promise to resolve (using either await or then). Going by the standard followed, it would be:
getCityName().then(city => {
console.log("City name is: "+city);
});
Just to add, since getCityName would returning a Promise which might reject, it would be sensible to add a catch block while calling the function as well.
I have tried to get a result but I can't find how to solve SyntaxError: await is only valid in async function
static async searchYoutube(query, message, voiceChannel) {
await youtube.searchVideos(query, 5).catch(videos => {
await message.say(
':x: There was a problem searching the video you requested!'
);
return;
});
if (videos.length < 5 || !videos) {
message.say(
`:x: I had some trouble finding what you were looking for, please try again or be more specific.`
);
return;
}
Now, they also recommended color async on the front, I did it but it marks another error
TypeError: Cannot read property 'length' of undefined
static async searchYoutube(query, message, voiceChannel) {
await youtube.searchVideos(query, 5).catch(async (videos) => {
await message.say(
':x: There was a problem searching the video you requested!'
);
return;
});
if (videos.length < 5 || !videos) {
message.say(
`:x: I had some trouble finding what you were looking for, please try again or be more specific.`
);
return;
}
Ad SyntaxError: await is only valid in async function – this is pretty much self explanatory error, however, there's no need to await for message.say(...) in either of these cases.
Ad TypeError: Cannot read property 'length' of undefined – this is because videos on line 8 is not defined anywhere but inside (and only inside of) that .catch block.
I'd recommend using .then block same way you use .catch in order to process searchVideos results. The code is asynchronous (is run concurrently), but the wrapper function itself doesn't need to be async.
static searchYoutube(query, message, voiceChannel) {
youtube.searchVideos(query, 5)
.then(videos => {
if (!videos || videos.length < 5) // first check !videos, then videos.length
message.say(`:x: I had some trouble finding what you were looking for, please try again or be more specific.`)
else {
... // continue your code
}
})
.catch(error => { // catch gives an error, not the result
message.say(':x: There was a problem searching the video you requested!')
})
}
I have a simple HTTP GET function that only needs to return the response in a function, but right now this function is returning void.
The code in sitechecks.js
var checkSite = () => {
https.get('https://itmagazin.info', (res, err) => {
if (res.statusCode === 200 && res.statusMessage === 'OK') {
return `The site returned: ${res.statusMessage}`
} else return `Error, the site returned: ${err.message}`
})
}
module.exports = checkSite
And when I import the module in index.js, the console returns [Function: checkSite] and not the value itself.
// Index.js
var extSiteCheck = require('./sitechecks')
// This console prints [Function: checkSite] instead of "OK"
console.log(extSiteCheck.checkSite)
However, if I add the return statement on http.get() in the function, the console prints undefined. So I thought that this undefined is a progress, but I don't understand why does it return undefined?
(return http.get() in the checkSite function)
Any help, tips is appreciated.
Because callbacks in JavaScript are asynchronous, you can't return from within a callback.
That means this
console.log(extSiteCheck.checkSite)
runs before the request comes back.
You can try console logging within your callback (instead of trying to return a value), in order to see this in practice. But basically, whatever you are trying to achieve with the results of your get request, you need to do inside the callback.
mebbe something like ... console.log( extSiteCheck.checkSite() );