mongoose

CastError: Cast to ObjectId failed for value “XXX” at path “_id” for model “Product”

只愿长相守 提交于 2021-01-01 21:25:49
问题 enter code here const express=require('express'); const router= express.Router(); const asyncHandler = require('express-async-handler'); const Product=require('../models/productModel'); router.get('/', asyncHandler(async (req,res)=>{ const products=await Product.find({}) res.send(products) })); router.get('/:id',asyncHandler(async(req,res)=>{ const product=await Product.findById(req.params.id) if(product){ res.json(product) }else{ res.status(404).json({message:"product not found"}) }`enter

CastError: Cast to ObjectId failed for value “XXX” at path “_id” for model “Product”

不问归期 提交于 2021-01-01 21:21:03
问题 enter code here const express=require('express'); const router= express.Router(); const asyncHandler = require('express-async-handler'); const Product=require('../models/productModel'); router.get('/', asyncHandler(async (req,res)=>{ const products=await Product.find({}) res.send(products) })); router.get('/:id',asyncHandler(async(req,res)=>{ const product=await Product.findById(req.params.id) if(product){ res.json(product) }else{ res.status(404).json({message:"product not found"}) }`enter

CastError: Cast to ObjectId failed for value “XXX” at path “_id” for model “Product”

夙愿已清 提交于 2021-01-01 21:20:11
问题 enter code here const express=require('express'); const router= express.Router(); const asyncHandler = require('express-async-handler'); const Product=require('../models/productModel'); router.get('/', asyncHandler(async (req,res)=>{ const products=await Product.find({}) res.send(products) })); router.get('/:id',asyncHandler(async(req,res)=>{ const product=await Product.findById(req.params.id) if(product){ res.json(product) }else{ res.status(404).json({message:"product not found"}) }`enter

Not able to connect to MongoDB atlas database

眉间皱痕 提交于 2021-01-01 13:53:24
问题 I am using A2 shared hosting server to host my node express application. I am giving the following URI to connect to mongoose. const uri = mongodb+srv://<username>:<password>@cluster0-gwff9.gcp.mongodb.net/customer-data?retryWrites=true&w=majority and the mongoose connect function is: mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true}).then(() => { console.log("Successfully connected to the database"); }).catch(err => { console.log('Could not

Is it possible to populate objects in map schema type?

↘锁芯ラ 提交于 2021-01-01 00:02:59
问题 I have schema type Map in my mongoose model. In this map, each element has reference to another model. I know that it's possible to populate attributes in array, but how about Map type? Be cause nesting like "map_type_attribute.some_attribute_to_populate" doesn't work. :) This is my model: const Mongoose = require('mongoose'); const parameter = Mongoose.Schema({ definition: { type: Mongoose.Schema.Types.ObjectId, ref: 'Definition', }, value: {}, }, {_id: false}); const schema = Mongoose

mongoose 实现 增、删、改、查

半腔热情 提交于 2020-12-30 04:31:58
mongoose常用的API 增 save是一个实例方法,使用时需要先 new Model() 来实例化 //保存一个用户信息,userobj为你创建的文档对象模型里的字段,需正确对应传入 const userobj={ email: query, passworld: req.body.passworld, hash: hash, isregister: false, score: 5, sign: [], signdate: '' } new db.MUser(userobj).save(function(error){ if (error) { res.status(500).send() return } res.json({statu: 200}) }) 删 remove 删除数据方法 db.Course.remove({_id: req.body.id}, function(err, docs){ if (err) { res.status(500).send(); return } res.json({statu: 200}) }) 改 update 更新数据方法 // 更新指定email字段数据条目下字段为content的内容,如果不存在就创建该字段 db.Share.update({email: email},{$set:{content: newarr}},

Incrementing a value with mongoose?

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-29 13:11:59
问题 I have a mongoose model in my node.js application, representing invoices. I have figured most of it out already, but I really need to ensure that my invoices are numerated/incremented to be able to provide a proper reference to my customer. Using an SQL database, I would have created an AUTO-INCREMENT column holding this value, but this obviosly isn't built into MongoDB. So how would I accomplish this with mongoose ? Here's how my model looks right now: var InvoiceSchema = new Schema({

【node】mongoose的基本使用

社会主义新天地 提交于 2020-12-29 10:40:10
1、安装mongoose npm install mongoose 2、启动数据库 mongod --dbpath d:\data\db 3、引入mongoose模块并连接数据库 const mongoose = require("mongoose"); mongoose.connect("mongodb://127.0.0.1:27017/test1",function(err) { if(err){ console.log('连接失败'); }else{ console.log("连接成功") } }); 4、创建表以及字段类型 const User = mongoose.model("user",{ name:String, age:Number }) 5、增 const user = new User({ name:"张三", age:19 }) user.save().then((result)=>{ console.log("成功的回调") },()=>{ console.log("失败的回调") }) 6、删 1、删除指定数据 User.remove({name:"zhao"}).then((result)=>{ console.log(result) }) result:是一个对象 返回值是受影响条数 2、删除所有数据 User.remove({}).then(

How to write a Mongoose model in ES6 / ES2015

落花浮王杯 提交于 2020-12-29 08:57:48
问题 I want to write my mongoose model in ES6. Basically replace module.exports and other ES5 things wherever possible. Here is what I have. import mongoose from 'mongoose' class Blacklist extends mongoose.Schema { constructor() { super({ type: String, ip: String, details: String, reason: String }) } } export default mongoose.model('Blacklist', Blacklist) I see this error in the console. if (!('pluralization' in schema.options)) schema.options.pluralization = this.options.pluralization; ^

Mongoose model Schema with reference array: CastError: Cast to ObjectId failed for value “[object Object]”

青春壹個敷衍的年華 提交于 2020-12-29 03:42:16
问题 I build a blog website with express.js and mongoosejs. A article may have one or more category. When I create a new article, I get error: { [CastError: Cast to ObjectId failed for value "[object Object]" at path "categories"] message: 'Cast to ObjectId failed for value "[object Object]" at path "categories"', name: 'CastError', type: 'ObjectId', value: [ [object Object] ], path: 'categories' } Could any one help me out? Related code shows bellow: The Article model defined like this: var