在我的 React 项目中,在一个组件中,我想包含一个 npm 模块,如下所示:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
这在常规 Node js 中确实有效,但是 React 显示了 TypeError: mongoose.connect is not a function
。尝试 import mongoose from 'mongoose'
返回 TypeError: mongoose__WEBPACK_IMPORTED_MODULE_4___default.a.connect is not a function
。
如何在 React 应用程序中使用 npm 包?我需要 meteor 吗(我听说过它,但我不明白它的作用)?
最佳答案
除了验证之外,您不能在浏览器中使用 mongoose => doc
Mongoose supports creating schemas and validating documents in the browser. Mongoose's browser library does not support saving documents, queries, populate, discriminators, or any other Mongoose feature other than schemas and validating documents.
关于javascript - 在 React 应用程序中导入 mongoose npm 模块时出现 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59934645/