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

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

问题描述

是的,我知道我应该从服务器端调用它.但是目的是从react-redux应用程序调用MongoDB strait.就像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

如何解决这个问题?

推荐答案

来自注释

猫鼬不能在前端工作,因为它依赖于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应用程序中导入猫鼬


Try importing mongoose in your react app

import mongoose from "mongoose";

并遍历其属性:

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天全站免登陆