gpt4 book ai didi

node.js - TypeError : User. findOne(...).than 不是函数

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:14 25 4
gpt4 key购买 nike

所以我正在学习 Brad Traversy Udemy 类(class) MERN Stack 从前到后:全栈 React、Redux 和 Node.js。

我收到奇怪的错误,说 findOne 不是一个函数...等等,我到处都找过,但我似乎找不到答案。请帮忙!

bcryptjs:2.4.3,
正文解析器:1.18.3,
快车:4.16.4,
头像:1.6.0,
jsonwebtoken:8.3.0,
Mongoose :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;

模型/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);

最佳答案

使用.then而不是.than

查看作为 findOne 结果的 Query 对象的文档,没有显示 .than 方法。 Query

关于node.js - TypeError : User. findOne(...).than 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53106871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com