Skip to content

Commit

Permalink
fix cors
Browse files Browse the repository at this point in the history
  • Loading branch information
blade8128ch committed Nov 6, 2023
1 parent 5df63ea commit f10c2ee
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const cors = require('cors')
const passport = require('./config/passport')
//const helpers = require('./_helpers')
const { getUser } = require('./_helpers')
Expand All @@ -15,6 +16,9 @@ app.use(express.json())

app.use(passport.initialize())
// use helpers.getUser(req) to replace req.user

app.use(cors())

app.use((req, res, next) => {
//res.locals.success_messages = req.flash('success_messages')
//res.locals.error_messages = req.flash('error_messages')
Expand Down
62 changes: 60 additions & 2 deletions controllers/apis/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const userController = {
.then((createdUser) => {
createdUser.toJSON()
delete createdUser.password
delete createdUser.checkPassword
return res.json({
status: 'success',
message: '註冊成功!',
Expand All @@ -51,8 +52,65 @@ const userController = {
}
},
getUser: (req, res, next) => {

}
const userId = req.params.id
// let tweetsCount=0
// let likesCount=0
// let followerCount=0
// let followingCount=0

User.findByPk(req.params.id, {})
.then(user => {
if (!user) throw new Error("User didn't exist!")
return user
})
.then(user => {
Promise.all([
Tweet.findAll({where: {userId } }),
Like.findAll({ where: { userId } }),
Followship.findAll({ where: { followerId:userId } }),
Followship.findAll({ where: { followingId:userId } })
])
.then(([tweetAll, likeAll,followerAll,followingAll]) => {
const tweetsCount=Object.keys(tweetAll).length
const likesCount=Object.keys(likeAll).length
const followerCount=Object.keys(followerAll).length
const followingCount=Object.keys(followingAll).length
console.log("===///////==",user,tweetsCount)
//
user=user.toJSON()
delete user.password
console.log("///////",tweetsCount)
user["followersCount"] = followerCount
user["followingCount"] = followingCount
user["likesCount"] = likesCount
user["tweetsCount"] = tweetsCount
return res.json({
status: 'success',
message: '查詢成功!',
...user
})
//console.log("0000",likesCount,"0000")
//console.log("00000" ,typeof(tweetAll) ,tweetAll,"00000" )
//console.log("11111" ,typeof(likeAll) ,likeAll,"11111" )
//return tweetsCount,likesCount,followerCount,followingCount
})
return user
})
// .then(user => {
// user=user.toJSON()
// console.log("///////",tweetsCount)
// user["followersCount"] = followerCount
// user["followingCount"] = followingCount
// user["likesCount"] = likesCount
// user["tweetsCount"] = tweetsCount
// return res.json({
// status: 'success',
// message: '查詢成功!',
// ...user
// })
// })
.catch(err => next(err))
},

}
module.exports = userController
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"body-parser": "^1.18.3",
"chai": "^4.2.0",
"connect-flash": "^0.1.1",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.16.4",
"express-session": "^1.15.6",
Expand Down

0 comments on commit f10c2ee

Please sign in to comment.