我找不到问题,我不知道逻辑是否不正确或缺少某些内容,请帮忙
const express = require('express');
const router = express.Router();
const { check, validationResult } = require('express-validator');
const auth = require('../../middleware/auth');
const request = require('request');
const config = require('config');
const key = config.get('WeatherKey');
const Post = require('../../models/Post');
const Profile = require('../../models/Profile');
const User = require('../../models/User');
以上所有呼叫
router.post(
'/',
[
auth,
[
check('cityname', 'cityname is required')
.not()
.isEmpty()
]
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
try {
const user = await User.findById(req.user.id).select('-password');
const cityname = req.body.name;
const url = `http://api.openweathermap.org/data/2.5/weather?q=${cityname}&units=imperial&appid=${key}`;
Api请求功能
request(url, (error, response, body) => {
if (error) console.error(error);
if (response.statusCode !== 200) {
res.status(404).json({ msg: 'No Temp Available' });
}
res.json(JSON.parse(body));
});
捕获错误
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
}
);
导出模块
module.exports = router;