I have a blogging web app. Whenever you create a post, a new instance of Post is created by the post controller. I want to make an API call to OpenWeatherMap and assign the temperature returned by the API call to the Post property this.data.weather.
This is the class Post
let Post = function (data, userid, requestedPostId) {
this.data = data
this.errors = []
this.userid = userid
this.requestedPostId = requestedPostId
}
The function that makes the API call is as follows:
Post.prototype.getWeather = function () {
return new Promise(async (resolve, reject) => {
let fetch_weather = await fetch(
"https://api.openweathermap.org/data/2.5/weather?id=6359002&appid=MY_API_KEY"
)
let weather_json = await fetch_weather.json()
console.log(weather_json.main.temp)
resolve(weather_json.main.temp)
})
}
I have tried to include this in the class declaration:
this.data.weather = this.getWeather()
The function runs successfully, and I can successfully access all the fields of the json returned and log them into the console, but this.data.weather is assigned a value of Object. I want this.data.weather to have a value of the temperature (which is what the getWeather() method returns).
PS: I'm using the npm package node-fetch that allows me to use fetch on the server the same way as with the native fetch API on the browser.
Edit after Roamer-1888's answer.
Below is the Post
model with the create
and cleanUp
methods. Before storing the post in the databse, the data passed to the Post
model is cleaned up and I am calling this.consumeWeather()
(I've renamed the letsConsumeAsynchronouslyDerivedWeatherData() method). Still, the model sends a value of undefined
to the database and I am at a loss here.
let Post = function (data, userid, requestedPostId) {
this.data = data
this.errors = []
this.userid = userid
this.requestedPostId = requestedPostId
}
Post.prototype.cleanUp = function () {
if (typeof this.data.title != "string") {
this.data.title = ""
}
if (typeof this.data.body != "string") {
this.data.body = ""
}
this.consumeWeather()
//Get rid of any bogus properties
this.data = {
title: sanitizeHTML(this.data.title.trim(), {
allowedTags: [],
allowedAttributes: [],
}),
body: sanitizeHTML(this.data.body.trim(), {
allowedTags: [],
allowedAttributes: [],
}),
createdDate: new Date(),
author: ObjectID(this.userid),
location: sanitizeHTML(this.data.location.trim()),
temp: this.data.temp,
}
}
Post.prototype.validate = function () {
if (this.data.title === "") {
this.errors.push("You must provide a title.")
}
if (this.data.body === "") {
this.errors.push("You must post content.")
}
}
Post.prototype.create = function () {
return new Promise((resolve, reject) => {
this.cleanUp()
this.validate()
if (!this.errors.length) {
//Save post into database if there are no errors
postsCollection
.insertOne(this.data)
.then((info) => {
resolve(info.ops[0]._id) //This promise resolves with the id of the post created so that it can be used by the controller to redirect to the newly created post upon saving it
})
.catch(() => {
this.errors.push("Please try again later")
reject(this.errors)
})
} else {
reject(this.errors)
}
})
}