从反应客户端调用猫鼬 [英] Calling mongoose from react client side

查看:27
本文介绍了从反应客户端调用猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道我应该从服务器端调用它.但目的是从 react-redux 应用程序调用 MongoDB 海峡.这就像 firebase 无服务器应用程序一样.我写

Yes, I know I should call it from server side. But the purpose is to invoke MongoDB strait from the react-redux app. It's like firebase serverless apps do. I write

    import mongoose from 'mongoose';

    let mongoDB = 'mongodb://127.0.0.1/my_database';
    mongoose.connect(mongoDB);

    mongoose.Promise = global.Promise;

    let db = mongoose.connection;
    db.on('error', console.error.bind(console, 'MongoDB connection error:'));

我得到:

TypeError: __ 
WEBPACK_IMPORTED_MODULE_6_mongoose___default.a.connect is not a function

如何解决这个问题?

推荐答案

来自评论 这里

Mongoose 无法在前端工作,因为它依赖于 Node 的功能,而浏览器 JS 实现中不存在这些功能.您不能将 Mongoose 导入前端代码.

Mongoose won't work in the frontend because it relies on functionality from Node which isn't present in browser JS implementations. You can't import Mongoose into frontend code.

<小时>

尝试在您的 React 应用中导入 mongoose


Try importing mongoose in your react app

从猫鼬"导入猫鼬;

并遍历其属性:

Object.getOwnPropertyNames(mongoose).forEach(prop => {
  console.log(prop);
});

你会得到

Promise
PromiseProvider
Error
Schema
Types
VirtualType
SchemaType
utils
Document

您需要使用 MongoDB 的方法,例如 connect,没有被导入.

The methods that you need to work with MongoDB, such as connect, are not being imported.

这篇关于从反应客户端调用猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆