简体   繁体   English

如何使用 Angular 使用 MongoDb

[英]How Can I use MongoDb with angular

I have a controller that I can use the functions in the template.我有一个控制器,我可以使用模板中的功能。 I am using IONIC, but I can't access the mongoDB, How can I do it ?我正在使用 IONIC,但我无法访问 mongoDB,我该怎么做? I have no idea how to do this.我不知道该怎么做。

$scope.InsertUser = function(){
    user = $("#NameId").val();
    db.users.insert(user);
}

so the log wrote "db is not defined", I agree that I cant use this on clientSide but Where is the serverSide ?.所以日志写了“db is not defined”,我同意我不能在客户端使用它,但是服务器端在哪里?。 my project directory tree:我的项目目录树:

enter image description here在此处输入图片说明

I've already saw a lot of videos or tutorials but is the same place I declare the line : db.users.insert(user);我已经看过很多视频或教程,但我在同一地方声明了这一行:db.users.insert(user);

I do not understand Where I need to declare my collections or where I can put my CRUD methods.我不明白我需要在哪里声明我的集合或我可以在哪里放置我的 CRUD 方法。

you need declare all of this in the server side.您需要在服务器端声明所有这些。 I'm not sure which sever side you uses ,but I uses play framework with java as server side.我不确定您使用哪个服务器端,但我使用 java 作为服务器端的播放框架。 What I do is triggers a GET request from angular with restAngular , to a function in the server side.我所做的是使用 restAngular 从 angular 触发一个 GET 请求,到服务器端的一个函数。 example:例子:

client side:客户端:

return: function(){
    restAngular.all("/get").get().then(function(result){
        $scope.array = result;
    })
}

(the GET request will get it response from this function)server side: (GET 请求将从这个函数得到它的响应)服务器端:

private final MongoClient mongoClient = new MongoClient();
private final MongoDatabase db = mongoClient.getDatabase("test");
private final MongoCollection<Document> usersCollection = MongoDatabase.getCollection("posts");
public static Result getResult(){
return usersCollection.find().sort(descending("date"))
                              .limit(limit)
                              .into(new ArrayList<Document>());
}

this is just a little example how to do it with angular as client side and java as server side.这只是如何使用 angular 作为客户端和 java 作为服务器端的一个小例子。 for more help you can comment her.如需更多帮助,您可以评论她。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM