Reactjs 需要帮助将游戏保存给用户';他最喜欢的游戏
我在尝试将保存的游戏与保存它的用户关联时收到错误。错误为“无法读取未定义的属性推送” 用户和游戏可以在控制台中阅读。我认为这可能与最初创建用户时的用户模型有关,但是我不能确定。我注意到如果我尝试Reactjs 需要帮助将游戏保存给用户';他最喜欢的游戏,reactjs,mongodb,express,mongoose,Reactjs,Mongodb,Express,Mongoose,我在尝试将保存的游戏与保存它的用户关联时收到错误。错误为“无法读取未定义的属性推送” 用户和游戏可以在控制台中阅读。我认为这可能与最初创建用户时的用户模型有关,但是我不能确定。我注意到如果我尝试console.log(user.favGames)它将返回未定义 我已经尝试了我能想到的一切,我已经写了大约10次控制器,都没有用 用户模型 const mongoose = require('mongoose') const bcrypt = require('bcrypt') const SALT_
console.log(user.favGames)
它将返回未定义
我已经尝试了我能想到的一切,我已经写了大约10次控制器,都没有用
用户模型
const mongoose = require('mongoose')
const bcrypt = require('bcrypt')
const SALT_ROUNDS = 6
const Schema = mongoose.Schema
const userSchema = new Schema(
{
username: { type: String, unique: true },
email: { type: String, unique: true, unique: true },
password: { type: String, required: true },
avatar: { type: String },
favGames: { type: Schema.Types.ObjectId, ref: 'Game', default: null },
comments: { type: Schema.Types.ObjectId, ref: 'Comment', default: null }
},
{
timestamps: true
}
)
userSchema.set('toJSON', {
transform: function(doc, ret) {
delete ret.password
return ret
}
})
userSchema.pre('save', function(next) {
const user = this
if (!user.isModified('password')) return next()
bcrypt.hash(user.password, SALT_ROUNDS, function(err, hash) {
if (err) return next()
user.password = hash
next()
})
})
userSchema.methods.comparePassword = function(tryPassword, cb) {
bcrypt.compare(tryPassword, this.password, cb)
}
module.exports = mongoose.model('User', userSchema)
博弈模型
const mongoose = require('mongoose')
const Schema = mongoose.Schema
let gameSchema = new Schema({
name: { type: String, required: true },
boxArtUrl: { type: String, required: true },
twitchID: { type: String, required: true },
comments: { type: Schema.Types.ObjectId, ref: "Comment"}
})
module.exports = mongoose.model('Game', gameSchema)
游戏路由器
const express = require('express')
const router = express.Router()
const gamesCtrl = require('../../controllers/gameCtrl')
function isAuthed(req, res, next) {
if (req.user) return next()
return res.status(401).json({ msg: 'Unauthorized ' })
}
router.get('/')
router.post('/', isAuthed, gamesCtrl.addGame)
module.exports = router
游戏控制器
const User = require('../models/user')
const Game = require('../models/Game')
function addGame(req, res) {
Game.create({
name: req.body.name,
twitchID: req.body.id,
boxArtUrl: req.body.box_art_url
})
.then(game => {
User.findById(req.user._id)
.then(user => {
console.log(game)
console.log(user.favGames)
// user.favGames.push(game)
// user.save()
})
.catch(err =>
console.log('error when updating user with new game', err)
)
})
.catch(err => console.log('error saving game', err))
}
module.exports = {
addGame
}
该错误在我的控制器中的
user.favGames.push(游戏)
处标记。请注意,当用户创建配置文件时,没有与其配置文件关联的游戏。我很确定我调用的是模型的实际数据实例,而不是模型本身。提前感谢您的帮助。看起来需要检查它是否存在:
User.findById(req.user._id)
.then(user => {
if (!Array.isArray(user.favGames)) {
user.favGames = [];
}
user.favGames.push(game);
user.save();
})
您的favGames(以及注释)必须像这样在用户模型中定义为数组
const userSchema = new Schema(
{
username: { type: String, unique: true },
email: { type: String, unique: true, unique: true },
password: { type: String, required: true },
avatar: { type: String },
favGames: [{ type: Schema.Types.ObjectId, ref: 'Game', default: null }],
comments: [{ type: Schema.Types.ObjectId, ref: 'Comment', default: null }]
},
{
timestamps: true
}
)
此外,user.save()还返回一个承诺,因此需要使用然后阻塞或等待
所以addGame函数必须是这样的(我将代码转换为async/await)
不幸的是,我仍然收到同样的错误。这几乎就像认为user.favGames是完全不存在的一样,req.user.\u id在中间件中,所以我应该很好。我正在使用JWTs进行身份验证。让我尝试一下异步wait的实现。您在用户模式中也是正确的,完全忽略了这一点。@SalvatoreArgentieri使用user.findById(req.user\u id)进行了操作?他们只是坐下来实现了您的建议。它确实可以与req.user.\u id一起工作。我已经设置好了它,这样我就可以从发送到服务器的请求中提取整个用户对象(我想这样会使事情变得更简单)。谢谢你的帮助!对于任何查看这篇文章的人,我想是什么用async await方法修复了它。再次感谢@SuleymanSah的帮助
async function addGame(req, res) {
try {
let game = await Game.create({
name: req.body.name,
twitchID: req.body.id,
boxArtUrl: req.body.box_art_url
});
let user = await User.findById(req.user._id);
if (user) {
user.favGames.push(game);
await user.save();
res.status(200).send("game and user saved");
} else {
console.log("user not found");
res.status(404).send("user not found");
}
} catch (err) {
console.log("Err: ", err);
res.status(500).send("Something went wrong");
}
}