2017-12-28 7 views
0

React, GraphQL 응용 프로그램과 함께 로컬 호스트 MongoDB 연결을 설정하려고합니다. 모든 것은 설정되어 있고 내 지식에 잘 작동합니다. 내 MongoDB Dbs를 시각적으로보기 위해 Compass를 설치했습니다.Localhost MongoDb를 App에 연결

Server.js 파일은 다음과 같다 :

const express = require('express'); 
const models = require('./models'); 
const expressGraphQL = require('express-graphql'); 
const mongoose = require('mongoose'); 
const bodyParser = require('body-parser'); 
const schema = require('./schema/schema'); 

const app = express(); 

//--- I added the localhost MongoDb link below ---// 

const MONGO_URI = 'mongodb://localhost:27017/graphql01/'; 
if (!MONGO_URI) { 
    throw new Error('You must provide a MongoLab URI'); 
} 

mongoose.Promise = global.Promise; 
mongoose.connect(MONGO_URI); 
mongoose.connection 
    .once('open',() => console.log('Connected to MongoLab instance.')) 
    .on('error', error => console.log('Error connecting to MongoLab:', error)); 

app.use(bodyParser.json()); 
app.use('/graphql', expressGraphQL({ 
    schema, 
    graphiql: true 
})); 

const webpackMiddleware = require('webpack-dev-middleware'); 
const webpack = require('webpack'); 
const webpackConfig = require('../webpack.config.js'); 
app.use(webpackMiddleware(webpack(webpackConfig))); 

module.exports = app; 

나는 mongod 서비스가 시스템에 벌금을 실행 확인할 수 있습니다. Compass를 통해 파일 내의 위 주소에 연결할 수 있습니다. npm initialisation 명령을 실행하면 앱이 정상적으로로드됩니다. 그러나 GraphiQL 인터페이스를 사용하여 돌연변이 명령을 실행하면 결국 오류가 발생합니다. TypeError: Failed to fetch.

여기에 누락 된 정보가 있으면 알려주십시오. 내가 바른 길에

답변

0

했다, 그것은 있었어야 :

const를 MONGO_URI = 'MongoDB를 : // localhost를 :/graphql01 27017';

IE : http를 mongodb &으로 바꾸고 끝에 슬래시를 사용하지 마십시오.

관련 문제