猫鼬+打字稿:TypeError:l.findOne不是函数

时间:2020-07-27 09:16:18

标签: typescript express mongoose

我在使用猫鼬的节点快速应用程序上迷失了很多时间……试图找出我错了地方...仍然一无所获... Stackoverflow是最后一次机会...非常接近放弃: /

我正在使用Mongoose,并且在开发模式下我没有任何问题,但是在编译到bundle.js进行生产后,从moongose获得的模型仅返回错误

  <router-link :to="{ name: 'polaroid', params: { canzone: canzone.path } }"> {{ canzone.nome }} </router-link>

DEV (一切正常(将打字稿用于文件夹体系结构,但主要使用js)

服务器文件夹/

  • tsconfig.json
  • entry.js
  • 动作/
    • users.js
  • mongodb /
    • index.js
    • 模型/
      • users.js

tsconfig

userLogin TypeError: l.findOne is not a function

mongodb / index.js

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "preserveConstEnums": true,
    "removeComments": true,
    "suppressImplicitAnyIndexErrors": false,
    "outDir": ".build",
    "allowJs": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "target": "es5",
    "declaration": true,
  },
  "exclude": [
    "./node_modules",
    "webpack.config.server.js",
    ".build"
  ],
  "include": [
    "**/*.ts",
    "**/*.js"
  ]
}

## actions / users.js

import mongoose from "mongoose";
import config from "../../../config";

export default async function() {

  try {
    await mongoose.connect(config.MONGO_URI, {
      useNewUrlParser: true,
      useFindAndModify: false,
      useCreateIndex: true,
      useUnifiedTopology: true,
    });

    console.log("Connection Succeeded");

  } catch {
    console.error.bind(console, "connection error")
  }

}

mongodb / models / users.js

import * as UserModel from "../../mongodb/models/users";

import { userPasswordVerification } from "../../services/account";

export const userLogin = async ({ args, actor }) => {

  const { input } = args || {};
  const { email, password } = input || {};

  const user = await UserModel.getUserByEmail(email);

  if(!user) {
    throw new Error("メールアドレスが正しくありません。もう一度お試しください。");
  }

  if (!userPasswordVerification(user, password)) {
    throw new Error("パスワードが正しくありません。もう一度お試しください。");
  }

  return {
    user
  }
}

关于开发,一切都很好! 我可以一直使用Model.findOne。 但是在将我的应用程序编译成捆绑文件之后... 我收到此错误

import mongoose from "mongoose";
import { getObjectId, getFormattedSchema } from "../utils";

import UserSchema  from "./schema/user";
const Model = mongoose.model("users", UserSchema);

export async function getUser(id, filters) {
  filters = getFormattedSchema(filters);


  return new Promise((resolve, reject) => {
    Model.findOne({
      _id: getObjectId(id),
      ...(filters || {})
    })
      .lean()
      .then((doc) => resolve(doc))
      .catch(() => reject())
  });
}

我的tsconfig文件错误?有人有什么主意吗? ...

如果我将Model.log写入console.log,我的功能将显示在开发中...

感谢您的阅读:)

0 个答案:

没有答案