0

i already have try to install mongodb via npm but i keep getting error Cannot find module "fs"

and my code is look like this

<script>
const MongoClient = require('mongodb').MongoClient;
	export default {
		data(){
			return{
				msg:'this is a test'
			}
		},
		created:function(){
			MongoClient.connect('mongodb://127.0.0.1:27017', (err, database) => {
				if (err){
					console.log('1');
				}else{
					console.log('2');
				}
			})
		}
	}
</script>
<template>
	<div>
		{{msg}}
	</div>
</template>

so how do i import mongodb to my vuejs 2 framework?

3
  • Vue runs in the browser. MongoDB runs on a server, it cannot run in a browser. What you are trying is technically impossible. Instead, you have to setup a web server (with expressjs, for example) which accesses MongoDB, and then communicate with between browser and server via AJAX. Commented Jun 15, 2017 at 21:12
  • got it, but my question is how to send data and receive data from mongodb? i know how to do this with express but i am new to vuejs. Commented Jun 15, 2017 at 21:23
  • You don't access mongodb from Vue code. You send AJAX/REST requests to your express server, which acesses mongodv and sends the result back to the client. You do so with any AJAX lib you want. I like to use axios. a nice tutotial can be found here: alligator.io/vuejs/rest-api-axios Commented Jun 15, 2017 at 21:33

1 Answer 1

2

VueJS is frontend framework.

You definitely should not try to deal with DB directly from Vue.

You should have backend made with any language/framework you want: NodeJS(if you want to stick with JS), ASP.NET(C#), Spring(Java) etc. and your backend should deal with DB and you should only make AJAX requests to your backend and send/get back JSONs and deal with JSONs on frontend with Vue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.