![](/img/trans.png)
[英]Removing Objects in one-to-many relationships using Node.js REST API, Mongoose, Express, Angular,
[英]Node Js rest services using mongoclinet
我是一名初級開發人員,正在使用Azure上的Node JS,Express,MongoDB開發REST服務。 在我開發的第一個休息服務中,我總是使用Mongoose開發的,但是現在我已經看到在Cosmos DB上使用Mongoose存在一些問題。 Cosmos DB連接Node js與MongoDb mongoclient一起使用。 我創建了一個db.js文件:
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://dbname:xxxxxxxx@dbname.documents.azure.com:10255/?ssl=true", function (err, db) {
db.close();
});
module.exports = mongoClient;
我已經定義了一個模型:
var mongoClient = require('../config/db.js');
var ProfileSchema = mongoClient.Schema({
companyName: { type: String, required: true },
firstname: {type: String, required: true},
address: {type: String, required: true},
city: {type: String, required: true},
state: {type: String, required: false},
postalcode: {type: String, required: true},
country: {type: String, required: true},
telephone: {type: String, required: true},
email: {type: String, required: true}
});
mongoClient.model('Profile', ProfileSchema);
module.exports = mongoClient.model('Profile');
廣告這是我的路由器:
var Profile = require('../models/Profile');
router.get('/profile', function(req,res){
Profile.find({}, (err, profile) => {
if (err) {
console.log(err);
return res.status(400).send({ status: 'ko', data: {msg: err.message }});
}
res.status(200).send({status: 'ok', data: {msg: 'List', profile: profile}});
});
});
現在,當我嘗試運行應用程序ai時,收到此錯誤:mongoClient.Schema不是函數,我不知道我的代碼是否還可以? 我該如何解決?
最好的謝謝
我認為您正在混合概念。 您想要做的是使用不支持此功能的MongoClient創建MongoDB模式。 為此使用貓鼬: https : //mongoosejs.com/docs/guide.html
MongoClient是MongoDB的基本驅動程序,我認為您不想直接使用它。
另外,在模型文件中,您將使用以下代碼:
var mongoClient = require('../config/db.js');
而且我不知道文件db.js中包含什么,但是我認為您沒有在其中實現自定義Schema()函數,這就是為什么會出現錯誤“ mongoClient.Schema不是一個函數”的原因。 要使此工作有效,請避免直接使用MongoClient並查看有關Mongoose的一些教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.