【问题标题】:TypeError: User.findOne(...).than is not a functionTypeError: User.findOne(...).than 不是函数
【发布时间】:2019-04-06 00:10:50
【问题描述】:

所以我正在学习 Brad Traversy Udemy 课程 MERN Stack Front To Back:Full Stack React、Redux 和 Node.js。

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

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);

【问题讨论】:

  • 你能改变 const User = require("../../models/User");到 const User = require("../../models/user");在你的 user.js

标签: node.js mongoose


【解决方案1】:

使用.then 而不是.than

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

【讨论】:

  • 天哪,你救了我!一个简单而愚蠢的错字让我发疯!
猜你喜欢
  • 2022-01-07
  • 2020-08-10
  • 2023-03-19
  • 2020-07-24
  • 1970-01-01
  • 2020-06-29
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多