Tag: mongoose

循环数据时如何使用多个callback

我试图获取HTML表单数据,通过循环,改变一下,并将其插入数据库。 我已经尝试像下面的app.js. 我怎样才能做callback,使formdata我修改了.create函数? 我从各处search,总是以某种方式结束了死胡同和未定义的variables。 app.js: //Find the day where to save Day.findById(req.params.id, function(err, day) { if (err) { console.log(err); res.redirect("/diary"); } else { // Search function to find data with _id function ingredientIdQuery(reqBodyId) { var ingQuery = Ingredient.find({_id:reqBodyId}); return dbQuery; } // This loops through HTML formdata and formats it for mongoose model for (var […]

如何用asynchronousvalidation更新mongoose模型(node / koa2)

从客户端,用户有一个表格填写他们可以更新他们的电子邮件或密码。 他们必须input当前密码才能validation请求。 之后,如果电子邮件地址正在被修改,那么电子邮件validation器应该运行以检查新的电子邮件地址是否已经被注册到另一个用户。 所以,在用户从客户端发送请求后,我得到以下信息: { userId: 'This is the ID of their entry in the database', currentPassword: 'Current password to check against one stored in database', newPassword: 'New password, will be blank if user has not requested a new password', email: 'Email address, will be the same if user has not requested a new email' […]

关于Node.js的问题 SChema和模型

关于Node.js的问题 架构,模型和npm。 我是node.js的新手,并按照步骤在mongoose文档中创build模式和模型。 http://mongoosejs.com/docs/ 我正在尝试使用kittens.js输出我在index.js中创build的模型,但我不确定是否正确。 有错误当我尝试编译时, C:\Atom\initial\index.js:11 db.on('error', console.error.bind(console, 'connection_error')); ^ TypeError: Cannot read property 'on' of undefined at Object.<anonymous> (C:\Atom\initial\index.js:11:3) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:394:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:509:3 我在index.js中有以下代码。 var express = require('express'); var mongoose = […]

匹配不与Mongoose中的聚合

没有匹配,我得到我需要的输出,当我提供$匹配值,我得到一个空的答复。 什么可能是空回应的原因? 有没有什么不同的方式来调用$匹配参数? var query = {"section":"324444"}; // 324444 is section schema _id value Transaction.aggregate({ "$match": query },{ "$group": { "_id": "$user", "section": { "$first": "$section"}, "amount": { "$sum": { "$cond": ["$credit", "$amount", { "$subtract": [ 0, "$amount" ] }] }} }}) .exec(function(err, transactions) { // Transactions List Grouped User wise }); // Transaction schema […]

mongoose – 铸造ObjectId失败

我正在一个小型的博客应用程序,我在这里被困在这个错误 – { "message": "Cast to ObjectId failed for value \" 597c4ce202ca9c353fc80e8a\" at path \"_id\" for model \"Blog\"", "name": "CastError", "stringValue": "\" 597c4ce202ca9c353fc80e8a\"", "kind": "ObjectId", "value": " 597c4ce202ca9c353fc80e8a", "path": "_id" } 我正在使用findById – 这是代码 app.get("/blogs/:id", function(req, res) { Blog.findById(req.params.id, function(err, foundBlog) { if (err) { res.send(err); } else { res.render("show", { blog: foundBlog }); } […]

什么是包含'ref'但不指定types的Mongoose模型属性?

对于mongoose来说很新奇 – 我正在做一个现有的项目,并且已经被赋予了改变一些模型属性的任务。 我明白,如果一个模型包含这种属性 postedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } 该属性引用另一个模型/模式,并访问该链接的模型/模式需要populate它来访问该属性。 但是在我正在审查的代码中(我没有写),这个types有很多属性 contentTypes: [{ ref: 'ContentType' }], source: { ref: 'Source',required: true }, 在哪里引用另一个模式,但没有types。 这是同一种关系,并暗示了id ? 这是一个子文件吗? 作为一个额外的问题:如果在一个模型中,我想引用一个链接模型(或模式)的属性,我需要先populate ? 也就是说,如果它是一个子文档,我可以使用点符号,但如果它是一个“链接”的文档,我不知道。

Mongoose,在更新中validation数组中的$ pull项

鉴于以下模式: var BookSchema = new Schema({ name: { type: String, required: true, unique: true }, description: { type: String, required: false }, authors: [AuthorSchema] }); var AuthorSchema = new Schema({ name: { type: String, required: true } type: { type: String, enum: ['FOO', 'BAR'], required: false } }, { _id: false }); 我想validation作者数组至less包含一个types=='FOO'的对象。 这意味着这应该失败: var […]

在模式文件中添加一个字段给出和错误,同时执行该文件

嗨,这是我已经有的模式文件。 // defining a mongoose schema // including the module var mongoose = require('mongoose'); // declare schema object. var Schema = mongoose.Schema; var blogSchema = new Schema({ title : {type:String,default:'',required:true }, subTitle : {type:String,default:''}, blogBody : {type:String,default:''}, tags : [],// name of tags in array created : {type:Date}, lastModified : {type:Date}, authorInfo : {} // […]

使用angular2 +服务编辑对象,mongoDB和NodeJs

我想对存储在数据库(mongoDB)中的数据进行基本编辑。 这是我所做的: angular度服务 createReview(review) { let headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('http://localhost:3000/api/reviews', JSON.stringify(review), { headers: headers }) .subscribe(res => { console.log(res.json()); }); } editReview(review) { let headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('http://localhost:3000/api/reviews', JSON.stringify(review), { headers: headers }) .subscribe(res => { console.log(res.json()); }); } deleteReview(id){ this.http.delete('http://localhost:3000/api/reviews/' + id).subscribe((res) => { console.log(res.json()); }); } 在server.js中 […]

聚合中的mongoose模式方法

怎样才能在聚合中访问方法? let mongoose = require("mongoose"), Schema = mongoose.Schema; let UserSchema = new Schema({ name: String, lastname: String }); UserSchema.methods.fullname = () => { return this.name + " " + this.lastname; }; let UserModel = mongoose.model("user",UserSchema); let query = [ { $match: { "name": /foo/i } } ]; UserModel.aggregate(query, (err, users) => { if(err) throw new […]