我整个上午都在谷歌搜索这个,并尝试各种修复,但我无法弄明白。
当我尝试运行时,我不断收到错误“TypeError:req.user.findOneAndUpdate不是函数”:
req.user.findOneAndUpdate({_id: req.user._id}, { $addToSet: { flashcards : { $each: cards }}}, {upsert : true}, function(err, doc) {
if(err) return console.log(err);
res.send(doc);
});
我已经尝试将req.user明确地转换为用户模型(例如var newUser = new User(req.body),尝试简化查询等但似乎没有任何工作。
编辑:使用模型声明
const User = require('../models/user');
var NewUser = new User(req.user);
NewUser.findOneAndUpdate({_id: req.user._id}, { $addToSet: { flashcards : { $each: cards }}}, {upsert : true}, function(err, doc) {
if(err) { return console.log(err); }
else { return res.send(doc);}
});
用户模型架构
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
FlashcardSchema = require('./flashcardSchema'),
bcrypt = require('bcrypt-nodejs');
var UserSchema = new Schema({
username: {
type: String,
required: false
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String
},
created_on: {
type: Date,
default: Date.now
},
points: Number,
flashcards: [FlashcardSchema],
courses: [{type: Schema.Types.ObjectId, ref: 'Course'}]
});
UserSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
UserSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
};
module.exports = UserSchema;
和模型
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
UserSchema = require('../schemas/userSchema');
module.exports = mongoose.model('User', UserSchema);
答案 0 :(得分:8)
解决方案是在模型上运行函数,而不是在它的实例上运行。所以而不是:
var NewUser = new User(req.user);
NewUser.findOneAndUpdate...
执行:
User.findOneAndUpdate...
答案 1 :(得分:0)
检查错误后你回来了 所以
//JavaScript Document
$('document').ready(function() {
var count = 1;
var total = 13;
$('.fa-caret-right').on('click' ,function scrollHorizontal() {
count++;
var number = count + " of 13";
var scrollTo = $("#p"+count);
var where = $(scrollTo).offset().left - 150;
$('#counter').html(number);
$(".gallery").stop().css("transform","translateX(-"+where+"px)");
}
);
});