I just recently asked this question below, I was referred to this question, and my question was closed. Fair enough. I read through that question prior though and after and still don't understand where I am too create a promise and resolve it. See I need to return a status and a message in this async function that wraps my code, and I can't return it inside the .then(data=>{console.log(data.success)}
Do I create a promise inside the .then, what I am trying to do is get the data.success into the res variable properly, instead of the way I am doing it which is creating an empty variable and assigning it in the .then. I know this is incorrect.
If someone could just point me in the right direction with some comment or something that would help. I have read the referred question, and see how it applies but don't know how to implement it in my context.
Initial Question snippet Below
I'm wondering if there is a better way than creating a let variable outside this fetch request, assigning it inside, and then using it later on. Here is my code so far. Is there a way to have the res variable return the value in the data.success inside the .then
let isItTrue
const res = await fetch(fetchUrl, {
method: "POST",
body: JSON.stringify({ message: "hello world" }),
headers: { "Content-Type": "application/json" },
})
.then(response => response.json())
.then(data => {
isItTrue = data.success
console.log(data)
console.log(data.success)
})
.catch(error => {
console.error("Error:", error)
})
if(isItTrue){
//DO SOMETHING
const isItTrueSomething = "success"
}
I am eventually returning something in the same scope as the if statement, this is all inside a async serverless function.
return {
statusCode: 200,
body: JSON.stringify({ message: isItTrueSomething }),
}
I want to return the status from fetch inside the return of this serverless function
require("dotenv").config()
const sgMail = require("@sendgrid/mail")
const {
SENDGRID_API_KEY,
SENDGRID_TO_EMAIL,
SENDGRID_TO_EMAIL_TEST,
} = process.env
const URL = require("url")
const https = require("https")
const fetch = require("node-fetch")
// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
const handler = async event => {
// try {
const body = JSON.parse(event.body)
let secret = process.env.GOOGLE_CAPTCHA_SECRET
const fetchUrl = `https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=${body.dataMassage.captcha}&remoteip=${event.headers["client-ip"]}`
console.log(fetchUrl)
const res = await fetch(fetchUrl, {
method: "POST",
body: JSON.stringify({ message: "hello world" }),
headers: { "Content-Type": "application/json" },
})
.then(response => response.json())
.then(data => {
// isItTrue = data.success
console.log(data)
console.log(data.success)
})
.catch(error => {
console.error("Error:", error)
})
// console.log(body)
const subject = event.queryStringParameters.name || "World"
return {
statusCode: 200,
body: JSON.stringify({ message: `FETCH_STATUS` }),
}
// } catch (error) {
// return { statusCode: 500, body: error.toString() }
// }
}
module.exports = { handler }