gpt4 book ai didi

angularjs - 当我进行 api 调用时,如何让数据库不附加状态?

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:58 25 4
gpt4 key购买 nike

所以我尝试使用 MEAN 堆栈创建一个简单的博客应用程序。我能够成功写入本地 mongo 数据库,但是当我调用数据库时,它会向我的查询附加附加信息,这使得 ng-repeat 变得困难。响应如下所示:

[{"_id":"135","title":"aaaaaa","body":"aaaaaa","__v":0,"posted":"2017-08-05T08:46:27.159Z"},
{"_id":"136","title":"bbbbb","body":"bbbbb","__v":0,"posted":"2017-08-05T08:46:40.232Z"}]
200
function (d){b||(b=vd(a));return d?(d=b[Q(d)],void 0===d&&(d=null),d):b}
{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/blogpost","headers":{"Accept":"application/json, text/plain, */*"}}
OK

我只想要对象中的前两个元素为“aaaa”和“bbbb”,这样我就可以使用 ng-repeat 来获取标题、正文等。这就是我在索引上显示信息的方式。 html:

<input ng-model="post.title" class="form-control" placeholder="title">
<textarea ng-model="post.body" class="form-control" placeholder="body"></textarea>
<button ng-click="createPost(post)" class="btn btn-primary btn-block">Post</button>
<div ng-repeat="post in posts">
{{post}}
</div>

这是我的 Angular 获取代码的样子:

    function getAllPosts() {
$http({
method: 'get',
url: '/api/blogpost'
}).then( function (posts){
$scope.posts = posts;
}, function (error){
console.error("Inside app.js._getAllPosts().error");
});
}

这是我的后端:

app.get("/api/blogpost", getAllPosts);

function getAllPosts(req, res) {
var post = req.body;
PostModel
.find()
.then(
function (post) {
res.json(post);
},
function (error) {
res.sendStatus(400);
}
);
}

如果需要,我可以发布其余的代码。

最佳答案

我认为您只需要引用 success callback 上的 data 属性即可:

}).then(function(response) {
$scope.posts = response.data;

关于angularjs - 当我进行 api 调用时,如何让数据库不附加状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45524473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com