From c5f6b053e3758e6b5518fe8d7d9b3eb7893c41c6 Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@inrae.fr>
Date: Thu, 5 Aug 2021 17:35:29 +0200
Subject: [PATCH] fix(get_hydrometrie_sites): potential issue if using the
 "fields" parameter

Refs #7
---
 R/get_hydrometrie_sites.R | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/R/get_hydrometrie_sites.R b/R/get_hydrometrie_sites.R
index c80f443..99deb37 100644
--- a/R/get_hydrometrie_sites.R
+++ b/R/get_hydrometrie_sites.R
@@ -37,22 +37,24 @@ get_hydrometrie_sites  <- function(params,
       )
     bFirst <- TRUE
     for (field in fields) {
-      fieldValue <- unique(unlist(x[[field]]))
-      if (unique_site && length(fieldValue) > 1) {
-        if(bFirst) {
-          warning(
-            "The site '",
-            x$code_site,
-            "' has ",
-            length(fieldValue),
-            " different locations, only the first one is returned",
-            call. = FALSE
-          )
-          bFirst <- FALSE
+      if (!is.null(x[[field]])) {
+        fieldValue <- unique(unlist(x[[field]]))
+        if (unique_site && length(fieldValue) > 1) {
+          if(bFirst) {
+            warning(
+              "The site '",
+              x$code_site,
+              "' has ",
+              length(fieldValue),
+              " different locations, only the first one is returned",
+              call. = FALSE
+            )
+            bFirst <- FALSE
+          }
+          fieldValue <- x[[field]][[1]]
         }
-        fieldValue <- x[[field]][[1]]
+        x[[field]] <- fieldValue
       }
-      x[[field]] <- fieldValue
     }
     x$geometry <- NULL
     x
-- 
GitLab