NodeJS - .save() 不是一个函数。无法通过猫鼬创建集合
Posted
技术标签:
【中文标题】NodeJS - .save() 不是一个函数。无法通过猫鼬创建集合【英文标题】:NodeJS - .save() is not a function. Unable to create collection via mongoose 【发布时间】:2017-05-08 21:11:34 【问题描述】:我也面临 .save() is not a function error,但我已经经历了大多数类似的 SO Questions 并尝试了他们各自的答案,但徒劳无功。但是通过我的终端,我可以创建收藏并保存一个。 .find() 也在工作。以下是我的代码:
模型 -> clientUser.js
var mongoose = require('mongoose');
var ClientUser = new mongoose.Schema(
firebaseUID :type: String,
userName : type : String,
displayName : type : String
);
ClientUser.index(firebaseUID: 1, unique: true);
module.exports =
ClientUser : ClientUser
;
dependency.js
'use strict';
var mongoose = require('mongoose');
var ClientUser = mongoose.model('ClientUser');
var getNewClientUserModel = function ()
return mongoose.model('ClientUser');
;
module.exports =
getNewClientUserModel : getNewClientUserModel
;
clientUser -> add.js
'use strict';
var dependency = require('./../../dependency');
var _ = dependency.getLodash();
var add = function (request, response)
var firebaseUID = request.body.firebaseUID;
var userName = request.body.userName;
var displayName = request.body.displayName;
var newClientUser= dependency.getNewClientUserModel();
newClientUser.firebaseUID = firebaseUID;
newClientUser.userName = userName;
newClientUser.displayName = displayName;
if (!firebaseUID || !userName)
response.send(400);
return null;
else
newClientUser.save(function (error,doc)
if (!_.isEmpty(error))
response.send(500, 'message': 'Error in adding the Category', error: error);
else
response.send(200);
return null;
);
;
module.exports =
add: add
;
app.js
var mongoose = require('mongoose');
mongoose.model('ClientUser', require('./models/clientUser').ClientUser);
/*
*Routes importing
*/
var ClientUser =
'add' : require('./routes/clientuser/add').add
;
【问题讨论】:
你能记录下“依赖”吗?因为看起来依赖路径不正确。否则代码看起来不错。 【参考方案1】:您需要将架构转换为您可以使用的模型,转换为您的 clientUser.js,然后将其传递给 mongoose.model(modelName, schema): p>
module.exports = mongoose.model('ClientUser', ClientUser);
【讨论】:
【参考方案2】:您的问题出在/models/clientUser.js
,您正在导出架构。我的建议是从该模块导出模型。另外我不明白dependency.js
的建议,为什么不在你的添加处理程序中直接使用模型?下面是一个应该可行的建议。
models/clientUser.js
var mongoose = require('mongoose');
var clientUserSchema = new mongoose.Schema(
firebaseUID :type: String,
userName : type : String,
displayName : type : String
);
clientUserSchema.index(firebaseUID: 1, unique: true);
var ClientUser = mongoose.model('ClientUser', clientUserSchema);
module.exports =
ClientUser : ClientUser
;
routes/clientuser/add.js
'use strict';
var ClientUser = require('./clientUser').ClientUser;
var add = function (request, response)
var firebaseUID = request.body.firebaseUID;
var userName = request.body.userName;
var displayName = request.body.displayName;
var newClientUser = new ClientUser(
firebaseUID: firebaseUID,
userName: userName,
displayName: displayName
);
if (!firebaseUID || !userName)
return response.send(400);
newClientUser.save(function (error, doc)
if (error)
return response.send(500, 'message': 'Error in adding the Category', error: error );
return response.sendStatus(200);
);
;
module.exports =
add: add
;
app.js
/*
*Routes importing
*/
var ClientUser =
'add' : require('./routes/clientuser/add').add
;
【讨论】:
以上是关于NodeJS - .save() 不是一个函数。无法通过猫鼬创建集合的主要内容,如果未能解决你的问题,请参考以下文章
无服务器框架、打字稿、nodejs 和 mysql - 错误:接收到的数据包顺序错误