【问题标题】:Adding/updating data to Parse.com using Javascript使用 Javascript 向 Parse.com 添加/更新数据
【发布时间】:2015-11-18 09:25:26
【问题描述】:

使用 Parse 时,如果我有一个名为 people 的对象和两列,一列称为 name,另一列称为 age。用户可以输入一个名称以匹配已存储在 Parse 中的名称,还可以输入要添加到该特定名称的年龄。如何让它搜索用户输入的名称,如果名称与用户输入的匹配,则将年龄添加到该特定名称?

【问题讨论】:

    标签: javascript parse-platform updating


    【解决方案1】:

    您无法在对象中保存任何内容,除非您有权访问其 objectId,因此您需要执行搜索以及保存。您需要找到与用户输入的姓名关联的对象,然后添加年龄值并保存。代码变成这样:

    var query = new Parse.Query("people");
    query.equalTo("name", inputName);
    query.find().then( function(objects) { // query and search for object(s)
        var person = objects[0]; // get the first object, there can be more than one obviously
        person.set("age", inputAge); // set the age
        return person.save(); // save the age value in object 
    }).then(function() {
        // success
    }, function(error) {
        // error
    });
    

    【讨论】:

    • 我试过了,但我不认为“var person = objects[0];”部分正在工作,因为它给了我错误“无法读取未定义的属性'set'”。
    • 这通常意味着查询没有找到与该名称匹配的对象。我写的代码 sn-p 天真地假设它至少返回一个对象。您需要测试 objects.length 属性以查看它是否像之前一样大于零
    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    相关资源
    最近更新 更多