我是mongodb的新手,正在使用 Express , Body-Parser 和 Mongoose ,并且我有一个mongodb数据库(在线,它的调用mlab)我从中提取数据的地方。一切正常,我已经使用邮差来获取,发布和删除。我试图获取JavaScript中的数据,以便可以在html中显示。我正在使用Ajax,它可以工作并返回成功,但是会触发Ajax的fail功能。
exports.photoalbum_all = function (req, res, next) {
PhotoAlbum.find(({}), function (err, photoalbum) {
if (err) return next(err);
res.send("../Views/images", {photo: photoalbum});
});
};
mongodb模型
const mongoose = require('mongoose');
const Schema = mongoose.Schema,
let PhotoAlbumSchema = new Schema({
title: String,
albums:[
{
u_name: String,
u_title: String
}]
},{
timestamps: true
});
PhotoAlbumSchema.virtual('pictureId').get(function(){
return this._id;
});
mongodb路线
const express = require('express');
const router = express.Router();
//
// Require the controllers
const photoalbum_controller =
require('../controllers/photoalbum.controller');
//
router.get('/find', photoalbum_controller.photoalbum_all);
app.js
// app.js
const express = require('express');
const bodyParser = require('body-parser');
const photoalbum = require('./routes/photoalbum.route'); // Imports routes for the photos
//
const app = express();
//
// Set up mongoose connection
const mongoose = require('mongoose');
let dev_db_url = 'mongodb://Trex_son:Salvat1on1987@ds243254.mlab.com:43254/photoalbumdb';
let mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, { useNewUrlParser: true });
mongoose.Promise = global.Promise;
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/photoalbums', photoalbum);
let port = 1234;
app.listen(port, () => {
console.log('Server is up and running on port number ' + port);
});
我不确定Ajax是检索此数据的最佳方法,是否有更好的方法将这些值检索到html