因此,我正在从头到尾上演Brad Traversy Udemy课程MERN Stack:全栈React,Redux和Node.js。
我收到一个奇怪的错误消息,说findOne不是一个函数... etc,我到处都看过,但是我似乎找不到答案。请帮忙!
bcryptjs:2.4.3,
正文解析器:1.18.3,
快递:4.16.4,
图形:1.6.0,
jsonwebtoken:8.3.0,
猫鼬:5.3.7,
护照:0.4.0,
护照-jwt:4.0.0,
验证者:10.8.0
TypeError: User.findOne(...).than is not a function
at router.post (/Users/jeno/Desktop/Scripting/TEST/MERN/routes/api/users.js:37:8)
at Layer.handle [as handle_request] (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/layer.js:95:5)
at /Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:335:12)
at next (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:174:3)
at router (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:317:13)
at /Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:335:12)
at next (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/Users/jeno/Desktop/Scripting/TEST/MERN/node_modules/body-parser/lib/types/json.js:101:7)
api / route / user.js
//USERS ROUTE. Everything that has to do with authentication, login...etc
const express = require("express");
const keys = require("../../config/keys");
const jwt = require("jsonwebtoken");
const passport = require("passport");
//Creating Router
const router = express.Router();
//Load User models
const User = require("../../models/User");
//Gravatar
const gravitar = require("gravatar");
//Password encryption
const bcrypt = require("bcryptjs");
//Use routes: When creating a router, instead of app.get, we use "router.get"
//@route GET api/users/test
//@description Tests users route
//@access Public (do not need log in)
router.get("/test", (req, res) =>
res.json({
msg: "USERS route is working!"
})
);
//@route GET api/users/register
//@description To register a user
//@access Public (do not need log in)
//@use mongoose to find if an email add already exist in database
router.post("/register", (req, res) => {
User.findOne({
email: req.body.email
}).than(user => {
if (user) {
return res.status(400).json({
email: "Email already exist!"
});
} else {
const avatar = gravitar.url(req.body.email, {
s: "200", //size
r: "pg", // rating
d: "mm" //default
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.than(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});
module.exports = router;
models / user.js
const mongoose = require("mongoose");
const schema = mongoose.Schema;
//Create User Schema
const UserSchema = new schema({
name: {
type: String,
require: true
},
email: {
type: String,
require: true
},
password: {
type: String,
require: true
},
avatar: {
type: String,
},
date: {
type: Date,
default: Date.now
}
});
module.exports = User = mongoose.model("users", UserSchema);