使用的 node+express+mongoose以上是我數(shù)據(jù)庫中的的數(shù)據(jù) 保存在local數(shù)據(jù)集中的indexes數(shù)據(jù)表里面我應(yīng)該如何通過mongoose來獲取其中的數(shù)據(jù)呢?假設(shè)我現(xiàn)在的路由為users 我該如何在users路由里面編寫代碼呢?代碼:var express = require('express');var assert = require('assert'); //引入斷言模塊var mongoose = require('mongoose');//導(dǎo)入mongoose模塊var db = mongoose.connection;var router = express.Router();// var Users = require('../modules/users');//導(dǎo)入模型數(shù)據(jù)模塊//首頁數(shù)據(jù)var data_home = { sub_tit: '12312', rec_txt: '12312321'};db.on('error', console.error.bind(console, 'connection error:'));db.once('open', function () { //數(shù)據(jù)表儲存文件的架構(gòu) var index_data = mongoose.Schema({ sub_tit: String, rec_txt: String }); //由Schema構(gòu)造生成的模型,除了Schema定義的數(shù)據(jù)庫骨架以外,還具有數(shù)據(jù)庫操作的行為,類似于管理數(shù)據(jù)庫屬性、行為的類 var data_true = mongoose.model('index', index_data); var fluffy = new data_true(data_home); fluffy.save(function (err, fluffy) { if (err) return console.error(err); router.get('/users', function(req, res, next) { res.json(fluffy); }); }); router.get('/users', function (req, res, next) { data_true.find(function (data,err) { res.json(data);//打印出來為null, res.json(err)//可以正確打印出來 這我就不懂了err不是錯(cuò)誤才會出現(xiàn)的嗎?難道是某個(gè)地方出錯(cuò)了? }); });});module.exports = router;正確的獲取方式是這樣的嗎?
如何通過 mongoose 獲取mongodb中的數(shù)據(jù)?
DIEA
2019-03-06 14:15:01