[英]Mongoose: TypeError: Cannot read property 'findOne' of undefined
[英]angularJS + mongoose: TypeError: Cannot read property 'model' of undefined
我有一個簡單的express應用程序,該應用程序應提供用於發布某些內容的端點。 在控制器中調用$ http時,出現以下錯誤:
TypeError:無法在C:\\ Webpages \\ veryNiceWords \\ API \\ api.js處讀取新模型(C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ mongoose \\ lib \\ model.js:738:17)中未定義的屬性'model' :24:17在Layer.handle [作為handle_request](C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ express \\ lib \\ router \\ layer.js:95:5)在下一個(C:\\ Webpages \\ veryNiceWords \\ API \\在Layer.handle處的Route.dispatch(C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ express \\ lib \\ router \\ route.js:112:3)處的node_modules \\ express \\ lib \\ router \\ route.js:131:13) [作為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。在Function.process_params(C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ express \\ lib \\ router \\ index.js:330:12)處的js:277:22在下一個(C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ express \\ lib \\ router \\ index.js:271:10)位於C:\\ Webpages \\ veryNiceWords \\ API \\ api.js:16:2位於Layer.handle [作為handle_request](C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ 在trim_prefix(C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ express \\ lib \\ router \\ index.js:312:13)中的Express \\ lib \\ router \\ layer.js:95:5)在C:\\ Webpages \\ veryNiceWords \\接下來在Function.process_params(C:\\ Webpages \\ veryNiceWords \\ API \\ node_modules \\ express \\ lib \\ router \\ index.js:330:12)處的API \\ node_modules \\ express \\ lib \\ router \\ index.js:280:7 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);
});
};
});
謝謝您的幫助。
var newWords = new Words.model({
應該有問題,應該是var newWords = new Words({
不需要new Words.model(
。僅需要new Words(
。
請更正代碼。
您希望您的'/ 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!');
});
});
您不需要額外的“模型”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.