Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有react.js的mongoose.connect()不起作用,并表示mongoose.connect()不是函数_Javascript_Node.js_Reactjs_Mongodb_Mongoose - Fatal编程技术网

Javascript 带有react.js的mongoose.connect()不起作用,并表示mongoose.connect()不是函数

Javascript 带有react.js的mongoose.connect()不起作用,并表示mongoose.connect()不是函数,javascript,node.js,reactjs,mongodb,mongoose,Javascript,Node.js,Reactjs,Mongodb,Mongoose,我正在使用node.js开发react.js应用程序。我想连接到同一网络中另一台计算机上托管的MongoDB数据库。我键入mongoose.connect(“mongodb://192.168.1.21:27017/testDB“,{useNewUrlParser:true,useUnifiedTopology:true})但是,当我运行react应用程序并在浏览器中打开它时 我得到这些错误 import React from 'react'; import Announcement fro

我正在使用node.js开发react.js应用程序。我想连接到同一网络中另一台计算机上托管的MongoDB数据库。我键入
mongoose.connect(“mongodb://192.168.1.21:27017/testDB“,{useNewUrlParser:true,useUnifiedTopology:true})


但是,当我运行react应用程序并在浏览器中打开它时


我得到这些错误

import React from 'react';
import Announcement from "../templates/Announcement.jsx"
const mongoose = require('mongoose')

mongoose.connect('mongodb://192.168.1.11:27017/realmDB', {useNewUrlParser: true});
const postSchema = new mongoose.Schema({
    title: String,
    content: String,
    author: String,
    postedOn: String,
    category: String
});

const Post = new mongoose.Model("Post", postSchema);

export default function() {
    const posts = Post.find({}, (err, post) => {
        return post
    })
    return <div>
        <div className="w3-card-4 w3-border w3-border-black inriasans mx-3" style={{marginTop: "1.5rem"}}>
            <header class="w3-container w3-light-gray w3-border-bottom w3-border-gray">
                <h3 class="inriasans"><i class="fa fa-bullhorn"></i> &nbsp;Announcements</h3>
            </header>
            <div class="w3-container pt-3 pb-3">
                <p class="w3-large">Get news related to NetherRealm here! You can also watch the <a href="#" className="w3-text-cyan">announcements</a> thread in the forums to get these announcements in your email.</p>
                <hr />
                {posts.map((post) => {
                    return <Announcement title={post.title} content={post.content} author={post.author} postedOn={post.postedOn} category={post.category} />
                })}
            </div>
            <footer class="w3-container w3-light-gray w3-border-top w3-border-gray pt-2">
                <p>View on the forums</p>
            </footer>
        </div>
    </div>
}
从“React”导入React;
从“./templates/Announcement.jsx”导入公告
const mongoose=require('mongoose')
猫鼬mongodb://192.168.1.11:27017/realmDB“,{useNewUrlParser:true});
const postSchema=新的mongoose.Schema({
标题:字符串,
内容:字符串,
作者:String,
波斯顿:弦,
类别:字符串
});
const Post=新的mongoose.Model(“Post”,postSchema);
导出默认函数(){
const posts=Post.find({},(err,Post)=>{
回程站
})
返回
公告

在此处获取与NetherRealm相关的新闻!您还可以在论坛中观看帖子,在电子邮件中获取这些公告


{posts.map((post)=>{
return是我的源代码。

你不能在客户端使用mongoose。请看:。还有其他选择吗?你应该设置一个节点服务器来连接Mongodb。直接从客户端连接到数据库不是一个好的做法。我对Node很陌生,可以给我一个例子吗?也许可以检查一下:你不能在客户端使用mongoose。看到了吗:。还有其他选择吗?您应该设置一个节点服务器来连接Mongodb。直接从客户端连接到数据库不是一个好的做法。我对Node很陌生,可以给我一个示例检查一下吗