nuxt-community / express-template

Starter template for Nuxt 2 with Express.
https://codesandbox.io/s/github/nuxt-community/express-template
1.25k stars 239 forks source link

Could example to use `Mongodb` in this? #125

Closed thearabbit closed 6 years ago

thearabbit commented 6 years ago

Could example to use Mongodb in this?

This question is available on Nuxt community (#c104)
ghost commented 6 years ago

This issue as been imported as question since it does not respect express-template issue template. Only bug reports and feature requests stays open to reduce maintainers workload. If your issue is not a question, please mention the repo admin or moderator to change its type and it will be re-opened automatically. Your question is available at https://cmty.app/nuxt/express-template/issues/c104.

thearabbit commented 6 years ago

Sorry, thanks

gamelaster commented 6 years ago

Sure you can, just put a mongoose initialization here: https://github.com/nuxt-community/express-template/blob/master/template/api/index.js Or generally in api folder

thearabbit commented 6 years ago

Thanks for your reply. I tried mongoose, and initial connect

import mongoose from "mongoose";
mongoose.connect("mongodb://localhost/meteor");
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('connected');
});

const { Router } = require("express");

const router = Router();

// Mock Users
const users = [{ name: "Alexandre" }, { name: "Pooya" }, { name: "Sébastien" }];

/* GET users listing. */
router.get("/users", function(req, res, next) {
  res.json(users);
});
...

But don't show console. I am not clear place to connection???

gamelaster commented 6 years ago

This will not work, because there is no support for import. Use require instead

thearabbit commented 6 years ago

I changed, by create connection.js and include it in to /api/index

// api/connection
var mongoose = require('mongoose')
mongoose
  .connect('mongodb://localhost:27017/meteor')
  .then(() => console.log('connection successful: meteor'))
  .catch(err => console.error(err))

var BookSchema = new mongoose.Schema({
  _id: String,
  name: String
})

module.exports = mongoose.model('Book', BookSchema)

And tried to find

// routes/user
// Model
var Book = require('../connection')

/* GET users listing. */
router.get('/users', function (req, res, next) {
  Book.find(function (err, result) {
    if (err) return next(err)
    console.log('Book', result)
  })

  res.json(users)
})

Still get

nuxt:build Generating /Users/theara/Desktop/test-app/nuxt-express/.nuxt files... +1ms
(node:1072) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
connection successful: meteor
  nuxt:build Generating files... +29ms
  nuxt:build Generating routes... +9ms
  nuxt:build Building files... +32ms
  nuxt:build Adding webpack middleware... +779ms
  ████████████████████ 100%

Build completed in 3.292s

 DONE  Compiled successfully in -7704ms                                                                    07:52:24

 OPEN  http://localhost:3000

  nuxt:render Rendering url / +0ms
  nuxt:render Data fetching /: 55ms +0ms
Book []

Bub books collection has data

thearabbit commented 6 years ago

But I tried to change db name connection -> otherName. It still show connection successful: meteor

thearabbit commented 6 years ago

Now work fine when change localhost -> 127.0.0.1