【问题标题】:Get _id of new Kinvey data on angular app在 Angular 应用程序上获取新 Kinvey 数据的 _id
【发布时间】:2023-04-02 17:10:01
【问题描述】:

当我在 Angular 中将新数据保存到 DataStore 时,我不想指定 _id。系统自动分配一个。通过查看网络跟踪,_id 在响应中传递回应用程序 - https://baas.kinvey.com/appdata/appxxx/activities/54ac3d8671e2d7933b0116b4 - 但无论如何我都没有看到在 Angular 文档中找到关于如何检索该 _id 的内容,因此我可以将其添加到现有列表或做其他处理。

    var promise = $kinvey.DataStore.save('activities', {
                text : $scope.newActivity.text ,
                duedate : '2015-01-01'
            });
         promise.then(
             function () {
                 $scope.newActivity.text = '';
             },
             function (error) {
                 //Kinvey insert finished with error
                 alert("Error insert: " + JSON.stringify(error));
             });

【问题讨论】:

    标签: angularjs kinvey


    【解决方案1】:

    Kinvey 实际上会在 Promise 中将对象返回给您,您只需从返回的对象中获取 _id。

    promise.then(function(data) {
        // Here's where you get your _id
        $scope.newActivity.text = data._id;
    }, function(err) {
        console.error(err);
    });
    

    【讨论】:

    • 谢谢你做到了,但是......我到底会在哪里找到这个?
    • 抱歉回复慢,@jhoskins98!看起来 Kinvey 试图在他们的 Concepts 页面上解释这一点,但我认为那里不是很清楚。当我刚开始时,我发现 Todd Motto 的 AngularJS tutorial 非常棒。我的工作项目大量使用了 Promise,以至于我很快就习惯了这个范式。还要研究链式 Promise,这可能非常有用——尽管要确保正确的数据落入正确的 Promise 中有点棘手。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2019-10-09
    相关资源
    最近更新 更多