简体   繁体   中英

mongoose model.findOne is not a function (Node.js | React Native)

So, I am making an app with react native and i am trying to use mongoose and mongoDB for my DataBase but i am running into this error: OD.findOne is not a function , OD being my model file.

const mongoose = require("mongoose");
const newOrgData = new mongoose.Schema({
author: String,
PW: String,
name: String,
Good: Number,
Bad: Number,
Medium: Number
});
module.exports = new mongoose.model("OrganizationData", newOrgData)

My DB.js file where i do the function:

const OD = require("../config/OrgData");
export async function GetStats(txt, point) {
await OD.findOne({
    name: txt
}, (err, data) => {
    if (txt.length <= 3) return Alert.alert("Please use a valid organization, the name needs to be over three characters long.");
    if (err) console.log(err);
    if (!data) {
        Alert.alert("Sorry, that organization does not exist.");
        return;
    } else {
        if (point == 1) {
            data.Good++;
            data.save();
        } else {
            if (point == 2) {
                data.bad++;
                data.save();
            } else {
                if (point == 3) {
                    data.medium++;
                    data.save();
                } else {
                    if (point == 0) {
                        return Alert.alert("Please choose how you feel about this organization today.");
                    } else {
                        Alert.alert("Succes, thank you for your interest.");
                    }
                }
            }
        }
    }
});
}

My server.js file where i connect to mongoose:

const Config = require("./config/config.json");

const mongoose = require("mongoose");

mongoose.connect("mongodb+srv://BlueSyntax:" + Config.Mongo_C_PW + 
"@org-qjoao.mongodb.net/" + Config.DB_Name + "?retryWrites=true&w=majority", {
useMongoClient: true
});

How should i fix this?

You have to omit new keyword when exporting the model. The correct form:

module.exports = mongoose.model("OrganizationData", newOrgData)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM