简体   繁体   中英

how to import mongo db in vue js 2?

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?

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.

fs (filesystem if i remember correctly) is used by node.js which should be run from your server.

Use it to connect to your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM