将数据文本文件添加到apigee数据库

时间:2015-05-18 11:14:40

标签: javascript node.js apigee

我正在尝试从文本文件向apigee数据库添加数据。我有几个包含书籍数据的文本文件。

我可以通过以下代码添加一本图书数据:

    function saveData(){
    //Initialize our client
    var client = new Usergrid.Client({
        orgName:"orgname",
        appName:"sandbox"
    });

    //In our options object we set the type of the entitiy
    //and we also set any data that goes along with it
    //in this case it's the books title.
    var options = {
        type:"book",
        title:"someTitle",
        author:"someAuthor",
        newElement:"Value"
    };

    //Let's create our entity, and display the results
    //of the creation in the html element with the id of response
    client.createEntity(options, function(error, book){
        if(error) {
            //error saving book
            $("#response").append("There was an error!");
            alert("Error");         
        } else {
            var uuid = book.get("uuid");
            var author = book.get("author");
            var title =  book.get("title");
            $("#response").append("Book saved! Its uuid on our server is: "+uuid+"<br/>");
            $("#response").append("The book you saved was: "+title + "<br/>");
            $("#response").append("It's author is: "+author);
            alert("Its working");            
        }
    }); 
}
saveData()

如何访问.txt文件并从javascript获取其他图书的数据? 我知道javascript只允许使用FileReader()通过用户输入添加文件。还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

您提供的代码不会生成任何.txt文件,但是为了阅读已保存的图书实体,您可以执行以下操作:

      books.fetch(
                function(err, data) { // Success
                    if (err) {
                        alert("read failed");
                    } else {
                        while(books.hasNextEntity()) {
                            var book = books.getNextEntity();
                            alert(book.get("title")); // Output the title of the book
                        }
                    }
                });