Javascript TypeError:.find(…)不是nodejs mongoose函数
我正在nodejs中处理样本。我被打字错误打动了:汽车。查找。。。这不是一个函数 当我尝试访问时,在D:\Karthikeyan\rest server\routes\carsRouter.js:19:15。谢谢 App.js routes/carsRouter.jsJavascript TypeError:.find(…)不是nodejs mongoose函数,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在nodejs中处理样本。我被打字错误打动了:汽车。查找。。。这不是一个函数 当我尝试访问时,在D:\Karthikeyan\rest server\routes\carsRouter.js:19:15。谢谢 App.js routes/carsRouter.js var express = require('express'); var bodyParser = require('body-parser'); /*Parses the data seerved from client*/
var express = require('express');
var bodyParser = require('body-parser'); /*Parses the data seerved from client*/
var mongoose = require('mongoose');
var Cars = require('../models/cars');
var carsRouter = express.Router(); /*with the help we have defined in global with router*/
carsRouter.use(bodyParser.json());
carsRouter.route('/')
.get(function(req,res,next){
Cars.find({})(function (err,cars) {
assert.equal(err,null);
res.json(cars);
});
})
.post(function(req,res,next){
Cars.create(req.body,function(err,cars){
if(err) throw err;
console.log('cars inserted');
var id = cars._id;
res.writeHead(200,{'content-type':'text/plain'});
res.end('the car you inserted is '+id);
});
})
.delete(function(req,res,next){
Cars.remove({},function(err,res){
if(err) throw err;
res.json(res);
});
});
carsRouter.route('/:carId')
.get(function(req,res,next){
Cars.findById(req.params.id,function(err,cars){
if(err) throw err;
res.json(cars);
});
})
.put(function(req,res,next){
Cars.findByIdAndUpdate(req.params.id,{
$set:req.body
},
{
new:true
},function(err,cars){
if(err) throw err;
res.json(cars);
});
})
.delete(function(req,res,next){
Cars.remove(req.params.id,function(err,res){
if(err) throw err;
res.json(res);
});
});
carsRouter.route('/:carId/comments')
.get(function(req,res,next){
Cars.findById(req.params.id,function(err,cars){
if(err) throw err;
res.json(cars.comments);
});
})
.post(function(req,res,next){
Cars.findById(req.params.id,function(err,cars){
if(err) throw err;
cars.comments.push(req.body);
cars.save(function (err,cars) {
if(err) throw err;
console.log('updated');
res.json(cars);
});
res.json(cars.comments);
});
})
.delete(function(req,res,next){
Cars.findById(req.params.id,function(err,cars){
if(err) throw err;
for (var i = (cars.comments.length - 1); i >= 0; i--){
cars.comments.id(cars.comments[i]._id).remove();
}
cars.save(function (err,cars) {
if(err) throw err;
console.log('updated');
res.json(cars);
});
});
Cars.remove(req.params.id,function(err,res){
res.writeHead(200,{'content-type':'text/plain'});
res.end('comments deleted');
});
});
module.exports = carsRouter;
models/cars.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var commentsSchema = new Schema({
rating:{
type:Number,
min:1,
max:5,
required:true
},
comment:{
type:String,
required:true
},
price:{
type:String,
required:true
}
},
{
timestamps:true
});
var carsSchema = new Schema({
name:{
type: String,
required:true,
unique:true
},
description:{
type:String,
required:true
},
comments:[commentsSchema]
},
{timestamps:true});
var cars = mongoose.model('cars',carsSchema);
module.exports = cars;
您以错误的方式使用了find查询。回调必须在find函数中
您以错误的方式使用了find查询。回调必须在find函数中
您的查找查询是错误的 更新如下:
Cars.find({}, function (err,cars) {
assert.equal(err,null);
res.json(cars);
});
或
您的查找查询是错误的 更新如下:
Cars.find({}, function (err,cars) {
assert.equal(err,null);
res.json(cars);
});
或
什么是汽车?JSON?不,我试图引用模型-var Cars=require'../models/Cars';什么是汽车?JSON?不,我试图引用模型-var Cars=require'../models/Cars';
Cars.find({}, function (err,cars) {
assert.equal(err,null);
res.json(cars);
});
Cars.find({})
.exec(function (err,cars) {
assert.equal(err,null);
res.json(cars);
});