阅读背景:

Angular 2和Cloudkit JS - 解决响应承诺

来源:互联网 

I'm trying to get Angular 2 work with Cloudkit JS, currently a simple Todo.

我正在努力让Angular 2与Cloudkit JS合作,目前这是一个简单的Todo。

todo.request.js:

todo.request.js:

var todorequest = {
    get: function(){
        var container = CloudKit.getDefaultContainer();
        var publicDB = container.publicCloudDatabase;

        var query = {
            recordType: 'todo'
        };

        return publicDB.performQuery(query).then(function (response) {
            if(response.hasErrors) {
                throw response.errors[0];
            } else {
                let vals = [];
                response.records.forEach(function(record){
                    vals.push({title: record.fields['title'].value, text: record.fields['text'].value, date: record.fields['date'].value})
                })
                console.log(vals)
                return vals;
            }
        })
    }
}

This returns Array [ Object, Object ] which is what I wanted, all good so far.

这返回了我想要的Array [Object,Object],到目前为止一切都很好。

todo.service.ts has the following:

todo.service.ts有以下内容:

import { Injectable } from '@angular/core';
import { Todo } from './todo';

declare var todorequest:any;

@Injectable()
export class TodoService {

    constructor() {}

    getTodos() {
        return new Promise<Todo[]>(resolve =>
            todorequest.get() as Todo[] 
            );
    }
}

I assume what I get from todorequest.get() is a promise, such as Object { Symbol(record)_1.k31kpejw2af: Object }. How do I convert it do Todo[], so I will be able to use the data? To be honest I'm not sure what I'm doing wrong.

我假设我从todorequest.get()获得的是一个承诺,例如Object {Symbol(record)_1.k31kpejw2af:Object}。我怎么转换它做Todo [],所以我将能够使用数据?说实话,我不确定我做错了什么。

1 个解决方案

#1


2  

Figured it out! I was missing resolve in service

弄清楚了!我在服务中失去了决心

getTodos() {
        return new Promise<Todo[]>(resolve =>
            resolve(todorequest.get())
            );
    }

分享到: