javascript - 在 React 应用程序中导入 mongoose npm 模块时出现 TypeError

标签 javascript node.js reactjs npm mongoose

在我的 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/

相关文章:

javascript - 如何使用 Javascript 更新选定的选项值

javascript - SVG 形状联合使用 javascript 或 d3 或像 Inkscape GUI 一样的 inkscape 脚本

javascript - 使用 Mongoose 分页包的模型错误

node.js - 无法从 NodeJS 中的任何提供者加载凭据

javascript - 如何在 React Native 中创建国家和城市下拉菜单

javascript - 如何设置日历应用程序的开始日期

Node.js - res.sendFile - 错误 : ENOENT, stat - 即使路径正确

reactjs - React Lazy 不适用于生产构建

reactjs - 是否可以将 Material UI 作为 peerDependency 但将其类型保留为 DevDependency?

javascript - 从 Canvas 中图像的不同点进行缩放/缩放