[英]I am receiving a "User.findOne is not a function" error message despite this same model working in another file. Similar question answers didn't work
First time posting on here, FYI.第一次在这里发帖,仅供参考。
Below is my "User" model in its own file.下面是我自己文件中的“用户”模型。 I create the schema and assign it.我创建架构并分配它。 When I go to use this model in my authRoutes file it works fine just as you'd imagine.当我在我的 authRoutes 文件中使用这个模型时,它可以正常工作,就像你想象的那样。 But when I use an identical import in one of my screen files I receive the "User.findOne is not a function" error message (linked below).但是,当我在其中一个屏幕文件中使用相同的导入时,我会收到“User.findOne 不是函数”错误消息(链接如下)。
I am using this method to find a user in the database in order to access it's attributes.我正在使用此方法在数据库中查找用户以访问其属性。 I have tried using the module.exports way of exporting the model and also trying to use require() as the import statement...neither has fixed my issue...been stuck on this for a while some insight would be great!我曾尝试使用 module.exports 导出模型的方式,并尝试使用 require() 作为导入语句......都没有解决我的问题......被困在这个问题上一段时间了,一些见解会很棒! (Hopefully this was clear, let me know) (希望这很清楚,让我知道)
"User.findOne is not a function..." “User.findOne 不是函数……”
models/User.js模型/User.js
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
balance: {
type: Number,
default: 100,
}
});
mongoose.model('userSchema', userSchema);
routes/authRoutes.js (Works here)路线/authRoutes.js(在这里工作)
const mongoose = require('mongoose');
const User = mongoose.model('userSchema');
const express = require('express');
const router = express.Router();
//const User = require('../models/User');
const jwt = require('jsonwebtoken');
router.post('/signup', async (req, res) => {
const {email, password} = req.body;
try {
const user = new User({email, password});
await user.save();
const token = jwt.sign({ userId: user._id}, 'SOME_SECRET_KEY');
res.send({token});
} catch (err) {
return res.status(422).send(err.message);
}
});
router.post("/signin", async (req, res) => {
const {email, password} = req.body;
if (!email || !password) {
return res.status(422).send({error: "Must provide email and password."});
}
const user = await User.findOne({ email });
if (!user) {
return res.status(422).send({ error: "Invalid email or password"});
}
try {
//await user.comparePassword(password);
const token = jwt.sign({userId: user._id}, "SOME_SECRET_KEY");
res.send({token, email});
} catch (err) {
return res.status(422).send({ error: "Invalid email or password"});
}
})
screens/SomeScreen.js (Does NOT work here, error) screen/SomeScreen.js(在这里不起作用,错误)
import React, {useContext, useState} from 'react'
import {View, StyleSheet, Text, ScrollView, TextInput, Button} from 'react-native';
//import { LinearGradient } from 'expo-linear-gradient';
import colors from '../../constants/colors';
import RecieverCard from '../../components/RecieverCard';
import HistoryCard from '../../components/HistoryCard';
import {Provider, Context} from "../context/AuthContext";
import AsyncStorage from "@react-native-async-storage/async-storage";
const mongoose = require('mongoose');
const User = mongoose.model('userSchema');
const getUserBalance = async (userEmail) => {
let user;
try {
user = await User.findOne(userEmail);
} catch(err) {
console.log(err);
}
return user;
};
require
is not a React api, and so it will not work in your frontend. require
不是 React api,所以它不能在你的前端工作。
You should use that in your node.js
backend.你应该在你的node.js
后端使用它。
Regardless, why would you use mongoose & models in your frontend?无论如何,你为什么要在你的前端使用猫鼬和模型? I suggest you make an endpoint / controller responsible of getting the user balance in your backend, and then simply send a request to your backend and await for the response data.我建议你让一个端点/控制器负责在你的后端获取用户余额,然后简单地向你的后端发送一个请求并等待响应数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.