0

我在 applab 中使用 Javascript。该代码可以很好地创建新用户,但它只更新第一条记录。任何想法如何解决这个问题,以便为每个新用户和返回用户更新?我想我需要使用带有“mostRecentID”或“record.ID”的数学函数来实现这一点。项目链接:https ://studio.code.org/projects/applab/bSkJw2auZx8Iq7ZGemsT7HwQrOVBWFPl6eLl0R0a6VU如果你去“它是如何工作的(查看代码)”,然后你可以看到代码和数据库。

//The database for this code already exists.
//This code creates new users fine, but it only updates the first record.
//What code will update a specific ID in the database 
//I think I need to use a math function with "mostRecentID" or  "record.ID" to achieve this.
var mostRecentID=1;
var player = {};
var UserID = getUserId();
onEvent("addNewRecordButton", "click", function() {
  readRecords("AllUserData", player, function(records) {
      var count = records.length;
      if (count > 1) {
          
      } else if ((count == 1)) {
          
      } else {
          var player = {};
          player.UserID = getUserId();
          player.Username=getText("usernameInput");
          createRecord("AllUserData", player, function(record) {
              mostRecentID=record.id;
          });
      }
  });
});
onEvent("updateInformationButton", "click", function() {
  updateRecord("AllUserData", {id:mostRecentID,UserID:getUserId(),Username:(getText("usernameInput"))}, function() {
    
  });
});
4

1 回答 1

1

假设您要更新与当前用户的用户 ID 匹配的记录。您需要在参数对象中指定用户 ID。

readRecords("AllUserData", {UserID: getUserId()}, function (records) {
  if (records.length > 0) {
    updateRecord("AllUserData", {id: records[0].id, UserID:getUserId(),Username:(getText("usernameInput"))}, function () {
      // callback code
    });
  } else {
    createRecord("AllUserData", {UserID:getUserId(),Username:(getText("usernameInput"))}, function () {
      // callback code
    });
  }
});
于 2021-11-10T04:53:17.607 回答