Tag: mongoose ejs

“未定义”使用多个callback函数(Node + EJS)

在我的app.js中,我有以下几点: app.get('/', function(request, response, next) { myLocation.find(function(err, locations) { if (err) { response.send(501, 'There was an error'); } else { response.render('index', { locations:locations }); } next(); }); app.get('/', function(request, response) { mySupplier.find(function(err, suppliers) { if (err) { response.send(501, 'There was an error'); } else { response.render('index', { suppliers:suppliers }); } }); }); }); 然后,我试图在我的index.ejs中显示位置和供应商的所有信息。 我可以同时工作,但不能同时工作。 […]

在HTML文件中显示存储在MongoDb中的图像

我正在使用Node.js + mongoose + EJS作为视图引擎。 我设法做一个简单的发送命令,检索并发送这个图像在服务器端使用本指南: 使用Multer.js在Express.js应用程序中安装file upload 用mongoose / express在mongodb中存储/显示图像 student.findOne({ 'username': req.body.student_username }, function(err, result) { result.img.data = fs.readFileSync(req.file.path); result.img.contentType = 'image/png'; result.save(); res.contentType(result.img.contentType); res.send(result.img.data); }) 然而,我正在努力寻找一个指导,我如何能够呈现一个页面,并把图像内的HTML。 我正在使用EJS视图引擎。 任何快速的例子或提示将受到欢迎。

在ejs中显示post的用户名

我正在使用ejs视图引擎创build我的第一个Node.js应用程序。 我有两种模式:发布和用户。 我试过将这两个模型链接在一起,但似乎无法弄清楚如何显示什么用户发布什么post! 这是用户模型: // User Schema var UserSchema = mongoose.Schema({ username: { type: String, index:true }, password: { type: String }, email: { type: String }, name: { type: String }, facebook : { id : String, token : String }, posts : [{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }] }); 这是post: // Post Schema […]

有没有什么办法来保存生成的EJS文件中的Jquery所做的HTML元素类更改?

我有一个待办事项列表节点项目,其中每个待办事项列表都保存为MongoDB中的实体,如下所示: var todoSchema = new mongoose.Schema({ items: [String] }); 当用户点击待办事项列表中的其中一个项目时,通过改变被点击的元素的CSS类别将其交叉出来。 当用户刷新时,这个类被改回到EJS文件如何呈现它。 我想把十字架放在本地,也就是说,只有划掉一个物品的人才会看到它被划掉。 有没有办法做到这一点,不涉及为每个用户分开数据库条目? ex entry: { "_id": { "$oid": "59bf2fed71c3840508539b29" }, "item": [ "ayy", "yyaa", "yyaaaafyyy" ], "__v": 0 }

Node.js / Mongoose /按顺序sorting和显示

我有一个小问题,以原始顺序显示结果集。 我的mongoose“查询”就是: myCollection.find({}, null, {sort: {weight: 1}}, function (err, res) { //etc… }); 水库的日志是好的,我的结果是按重量顺序正确sorting。 我用一个forEach循环在ejs模板中显示结果如下: <% res.forEach(function(r) { %> <%= r.weight %> <% }) %> 它以“伪随机”顺序显示我的结果…例如:2,3,4,1,9,10,6 … 在我的模式中,权重参数是一个数字 weight : { type: Number, default: 0, required: false } 更新: 我对结果执行另一个查询,像这样的循环: newResult = []; async.each(res, function(r, callback) { // some actions with r … newResult.push(r); }, […]

循环访问具有子数组的embedded式文档,并将其显示在EJS文档上

我的目标是,我试图通过一个embedded的文档循环显示他们相同的ID职位的评论。 这是我在node.js服务器中的Schema结构: var postSchema = new mongoose.Schema({ name: String, post: String, comment: [ { commentname: String, comment: String } ] }); 以下是我如何收到添加注释到具有当前ID的post: app.post('/comment/:id', function(req, res) { console.log(req.body); var comments = { commentname: req.body.name, comment: req.body.comment } Posts.update({_id: req.params.id}, {$push: {comment: comments}}, {safe: true, upsert: true}, function(err, comments){ if (err) { console.log("Issue with adding a comment"); […]

如何将HTML中的select选项与两个预定义的值绑定,以在mongodb中插入数据

我在我的pipe理仪表板中创build一个事件创build器,并且一切正常,但现在我需要插入一个新的字段: info: Object 在我的HTML事件创build者,我有这个select: <label for="info">Information Type:</label> <select id="info" name="info"> <option>Normal</option> <option>Special</option> </select> 随着一个职位/create 当这个事件到达我的控制器,我需要保存在我的数据库中的所有表单input,并很好,这部分是100%的工作。 但是,在进行保存之前,我使用此代码将select选项与数据库中字段info的实际值绑定: event.pre('save', function(next) { if (this.info === 'Normal') { this.info = ['this text', 'is one text, but i want show as a list', 'that's why i am doing this'] } else if (this.info === 'Special') { this.info = ['same', 'happens', […]

我怎样才能让ejs和mongoose一起工作?

我有一个路线,获取所有的客户端,我试图通过客户端ejs呈现它们。 我收到以下错误: Express 500 SyntaxError: Unexpected token { at Object.Function (unknown source) at exports.compile (/Users/sm/Desktop/AttApp/node_modules/ejs/lib/ejs.js:234:12) at Object.exports.render (/Users/sm/Desktop/AttApp/node_modules/ejs/lib/ejs.js:273:10) at View.exports.renderFile [as engine] (/Users/sm/Desktop/AttApp/node_modules/ejs/lib/ejs.js:303:22) at View.render (/Users/sm/Desktop/AttApp/node_modules/express/lib/view.js:75:8) at Function.app.render (/Users/sm/Desktop/AttApp/node_modules/express/lib/application.js:503:10) at ServerResponse.res.render [as partial] (/Users/sm/Desktop/AttApp/node_modules/express/lib/response.js:721:7) at ServerResponse.module.exports.res.render (/Users/sm/Desktop/AttApp/node_modules/express-partials/index.js:55:9) at ServerResponse.res.renderPjax (/Users/sm/Desktop/AttApp/node_modules/express-pjax/pjax.js:17:11) at Promise. (/Users/sm/Desktop/AttApp/app.js:61:17) 这是我的路线: app.get( '/clients', function( req, res ) { return ClientModel.find( function( err, […]

如何更新在ejs + mongoose中有多个值的表单?

用户在mongoose中有字段,如果用户决定更新,这些字段将被更新。 这是用户架构 var User = Schema({ education: [{ type: String}], }); 所以基本上用户可以更新或添加字段,例如用户可以使用表单添加额外的教育和技能信息。 我如何在ejs和路线中正确地做到这一点? 我在route.js中的尝试 router.post('/update-resume', function(req, res) { User.findById(req.user._id, function(err, foundUser) { // This part how do I update ? if (req.body.education) foundUser.resume.education.push(req.body.education); foundUser.save(); }); }); 价值不断推动,我想,我知道很明显,我正在推动数据到现场,但如何正确地更新它? Form.ejs <div class="form-group"> <label for="education">Education:</label> <% for(var i = 0; i < user.resume.education.length; i++) { %> <input type="text" […]

为什么明确告诉我,我的默认视图引擎没有定义?

我正在使用nodejs和mongodb在后端为我正在工作的应用程序。 我正在使用快递testing应用程序,我试图使用ejs呈现我的HTML文件。 但是,我有我的默认视图引擎没有被定义的问题。 这是我的app.js: /** * Module dependencies. */ var express = require('express') , routes = require('./routes') , user = require('./routes/user') , http = require('http') , path = require('path'); var conf = require('./conf'); var app = express(); var mongoose = require('mongoose'); , Schema = mongoose.Schema , ObjectId = mongooseSchemaTypes.ObjectID; var UserSchema = new Schema({}) , […]