angularJS + mongoose:TypeError:无法读取未定义的属性“模型”
Posted
技术标签:
【中文标题】angularJS + mongoose:TypeError:无法读取未定义的属性“模型”【英文标题】:angularJS + mongoose: TypeError: Cannot read property 'model' of undefined 【发布时间】:2016-06-19 08:58:06 【问题描述】:我有一个简单的快速应用程序,它应该提供一个端点来发布一些内容。在我的控制器中调用 $http 后,我收到以下错误:
TypeError:无法读取未定义的属性“模型” 在新模型(C:\Webpages\veryNiceWords\API\node_modules\mongoose\lib\model.js:738:17) 在 C:\Webpages\veryNiceWords\API\api.js:24:17 在 Layer.handle [as handle_request] (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\layer.js:95:5) 在下一个(C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\route.js:131:13) 在 Route.dispatch (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\layer.js:95:5) 在 C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:277:22 在 Function.process_params (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:330:12) 在下一个(C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:271:10) 在 C:\Webpages\veryNiceWords\API\api.js:16:2 在 Layer.handle [as handle_request] (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:312:13) 在 C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:280:7 在 Function.process_params (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:330:12) 在下一个(C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:271:10) 在 C:\Webpages\veryNiceWords\API\node_modules\body-parser\lib\read.js:129:5
我不知道这意味着什么,或者如何解决它。有人可以看看我的代码并指出我做错了什么吗?
api.js(快递应用)
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Words = require('./models/words.js');
//initialize our express app
var app = express();
//use body parser with JSON
app.use(bodyParser.json());
//middleware for CORS requests
app.use(function(req, res, next)
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
);
//register endpoint
app.post('/API/addWords', function(req, res)
//get user from request body
var words = req.body;
var newWords = new Words.model(
author: words.author,
source: words.source,
quote: words.quote
);
newWords.save(function(err)
if (err) throw err;
console.log('words saved!');
);
);
//connect to mongoDB
mongoose.connect('');
//define our server
var server = app.listen(3000, function()
console.log('api listening on ', server.address().port);
);
words.js(api需要的模型)
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var wordsSchema = new Schema(
author: String,
source: String,
quote: String
);
var Words = mongoose.model('Words', wordsSchema);
// make this available to our users in our Node applications
module.exports = Words;
前端html:
<div class="container-fluid">
<div class="row">
<div class="form-group">
<form ng-submit="submit()">
<label for="author">Author:</label>
<input type="text" class="form-control" id="author" ng-model="author" placeholder="Maya Angelou">
<br>
<label for="source">Source:</label>
<input type="text" class="form-control" id="source" ng-model="source" placeholder="I know why the caged bird sings">
<br>
<label for="quote">Quote:</label>
<textarea class="form-control" rows="10" id="quote" ng-model="quote" placeholder="There is no greater agony than bearing an untold story inside you"></textarea>
<br>
<div class="col-xs-4 col-xs-offset-4">
<input class="btn btn-default postQuote" type="submit" value="Quote it"></div>
</form>
</div>
</div>
</div>
<div class="container">
<p> author </p>
<p> source </p>
<p> quote </p>
</div>
前端控制器:
'use strict';
angular.module('frontEndApp')
.controller('AddwordsCtrl', function($scope, $http)
$scope.submit = function()
var url = 'http://localhost:3000/API/addWords';
var words =
author: $scope.author,
source: $scope.source,
quote: $scope.quote
;
$http.post(url, words)
.success(function(res)
console.log('posted quote!');
)
.error(function(err)
console.log(err);
);
;
);
感谢您的帮助。
【问题讨论】:
【参考方案1】:var newWords = new Words.model(
有问题,应该是var newWords = new Words(
new Words.model(
不是必需的。只需要new Words(
。
请更正代码。
【讨论】:
【参考方案2】:您希望“/API/addWords”的控制器端点如下所示:
app.post('/API/addWords', function(req, res)
//get user from request body
var words = req.body;
var newWords = new Words(
author: words.author,
source: words.source,
quote: words.quote
);
newWords.save(function(err)
if (err) throw err;
console.log('words saved!');
);
);
您不需要额外的“模型”。
【讨论】:
以上是关于angularJS + mongoose:TypeError:无法读取未定义的属性“模型”的主要内容,如果未能解决你的问题,请参考以下文章
无法通过 express/mongoose/angularjs 从 Mongodb 访问数据
AngularJS SPA 使用 API 和 Mongoose
如何使用 Mongoose 在 AngularJS 中创建 PUT 函数
将新评论推送到 mongoose/mongodb/angularjs 中的评论数组中
如何在 Mongoose 中更新/更新文档/数据? Passport MongoDB、Express、AngularJS、Nodejs