我也面临 .save()不是一个功能错误,但我已经完成了大部分类似的SO问题,并尝试了各自的答案,但徒劳无功。但通过我的终端,我可以创建集合并保存一个。此外 .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
};
答案 0 :(得分:0)
您的问题在/models/clientUser.js
,您正在导出架构。我的建议是导出一个模型表单。另外我不理解dependency.js
的建议,为什么不在你的添加处理程序中直接使用模型呢?以下是一个应该有效的建议。
<强>模型/ 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
};
<强>路由/ 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
};
答案 1 :(得分:0)
您需要将架构转换为可以使用的模型,转换为 clientUser.js ,然后将其传递给 mongoose.model(modelName,schema):
get