值不是字符串 - StrongLoop

Posted

技术标签:

【中文标题】值不是字符串 - StrongLoop【英文标题】:Value is not a String - StrongLoop 【发布时间】:2021-12-17 13:23:48 【问题描述】:

我正在使用强循环来创建 api。但这给了我错误。我的 json 文件的属性是:

"properties": 
    "id": 
      "type": "Number"
    ,
    "name": 
      "type": "string",
      "required": true
    ,
    "language": 
      "type": "string",
      "required": false
    ,
    "timezone": 
      "type": "string",
      "required": false
    ,
    "labelId": 
      "type": "number",
      "required": false,
      "default": 0
    ,
    "street": 
      "type": "string",
      "required": false
    ,
    "contact": 
      "type": "number",
      "required": false
    ,
    "maincontact": 
      "type": "number",
      "required": false
    ,
    "visitorTypes": 
      "type": "array",
      "required": false
    ,
    "activeVisitorAvatar": 
      "type": "boolean"
    ,
    "activeLegalDocument": 
      "type": "boolean"
    ,
    "legalDocuments": 
      "type": "array"
    ,
    "logo": 
      "type": "string",
      "required": false
    ,
    "logoType": 
      "type": "string",
      "required": false
    ,
    "logoSmall": 
      "type": "string",
      "required": false
    ,
    "logoSmallType": 
      "type": "string",
      "required": false
    ,
    "activeSignOut": 
      "type": "boolean"
    ,
    "activePrint": 
      "type": "boolean"
    ,
    "activeScanTemperature": 
      "type": "boolean"
    ,
    "printerIp": 
      "type": "string"
    ,
    "activeVoicePrompt": 
      "type": "boolean"
    ,
    "mandatoryCompanyName": 
      "type": "boolean"
    ,
    "mandatoryPhoneNumber": 
      "type": "boolean"
    ,
    "sliders": 
      "type": "array"
    ,
    "slidersCount": 
      "type": "number"
    ,
    "accountId": 
      "type": "number"
    ,
    "visitorsignouttime": 
      "type": "number"
    ,
    "signoutLink": 
      "type": "boolean"
    ,
    "signoutnotification": 
      "type": "boolean"
    ,
    "deviceofflinenotification": 
      "type": "boolean"
    ,
    "deviceonlinenotification": 
      "type": "boolean"
    ,
    "emergencyAlert": 
      "type": "object"
    ,
    "autoSignOut": 
      "type": "boolean"
    ,
    "signOutTime": 
      "type": "string"
    ,
    "signOutPin": 
      "type": "boolean"
    ,
    "questionsEnabled": 
      "type": "boolean"
    ,
    "questions": 
      "type": "array"
    ,
    "logicalQuestionnaire": 
      "type": "array"
    ,
    "isEnableTemperatureCheck": 
      "type": "boolean"
    ,
    "disableTemperatureCheckScreen": 
      "type": "boolean"
    ,
    "isEnableQrCodeWithPinInside": 
      "type": "boolean"
    ,
    "isEnableComplianceAlerts": 
      "type": "boolean"
    ,
    "alertsWatchlistPriority": 
      "type": "number"
    ,
    "alertsCompliancePriority": 
      "type": "number"
    ,
    "eventNames": 
      "type": "array"
    ,
    "rooms": 
      "type": "array"
    ,
    "selfie": 
      "type": "boolean"
    ,
    "displayCompany": 
      "type": "boolean"
    ,
    "pagination": 
      "type": "boolean"
    
  ,

我的身体如下:

let body = JSON.stringify(
      id,
      name,
      street,
      timezone: this.timezone,
      activeVisitorAvatar,
      activeLegalDocument,
      legalDocuments,
      visitorTypes,
      activeSignOut,
      activeScanTemperature,
      activePrint,
      printerIp,
      labelId,
      activeVoicePrompt,
      mandatoryCompanyName,
      mandatoryPhoneNumber,
      mandatoryAnswersToQuestions,
      visitorsignouttime,
      signoutLink,
      signoutnotification,
      deviceofflinenotification,
      deviceonlinenotification,
      emergencyMessages: this.getEmergencyMessages(),
      address: this.address,
      autoSignOut,
      signOutTime,
      signOutPin,
      questionsEnabled,
      questions: this.getQuestions(),
      logicalQuestionnaire: this.getLogicalQuestionnaire(),
      accountId,
      isEnableRememberMeForFullUIFlow,
      isEnableTemperatureCheck,
      isEnableQrCodeWithPinInside,
      isEnableComplianceAlerts,
      alertsWatchlistPriority,
      alertsCompliancePriority,
      selfie,
      displayCompany,
      temperatureThreshold: 
        maximum: Number(temperatureMax),
        minimum: Number(temperatureMin),
        displayTextFormat: this.curr.temperatureThreshold.displayTextFormat,
      ,
      pagination,
      gdpr: 
        isActive: this.curr.gdpr.isActive,
        days: gdprDays,
      ,
      purposes /*filter(purposes, (item) => item.id)*/,
      isEnableIfThenQuestionnaire: isEnableIfThenQuestionnaire,
      visitorQueueDisplay,
      autoRefreshEntries:  enabled: autoRefreshEntries, interval: Number(autoRefreshEntriesInterval) ,
      visitor_email,
    );

但我收到错误"error":"statusCode":400,"name":"Error","message":"Value is not a string.","stack":"Error: Value is not a string.\n at Object.validate

我被困在这里了!已尝试更改 json 文件中的数据类型,但没有帮助也无法判断是哪个参数导致错误。所有值都来自前端 html 和 angular。我的依赖如下:

"loopback": "^3.0.0",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^2.4.0",
"loopback-component-storage": "^3.0.0",
"loopback-connector-mongodb": "3.0.1",

我正在进行的网络请求调用是:

try 
      this.http
        .post('/api/sites/edit?access_token=' + this.token, body, 
          headers: contentHeaders,
        )
        .subscribe(
          (response) => 
            this.hideFlag = true;
            this.toastr.success('Saved!');
            this.http
              .post(
                'api/users/setFeaturesByAccountId',
                ,
                
                  params: 
                    access_token: this.token,
                    listFeatures: this.listFeature,
                    accountId: this.curr.accountId,
                  ,
                ,
              )
              .subscribe();
          ,
          (error) => 
            this.hideFlag = true;
            this.toastr.error('Error');
            this.showError = error.json().error;
            console.log(error.text());
          ,
        );
     catch (error) 
      console.log(error);
    

【问题讨论】:

你能分享负责自己发出网络请求的代码 name的值是多少 @Ayzrian OG 站点是前端传递的值 @AbirTaheer 我已将网络调用添加到问题中。 您将body 变量定义为字符串化的json let body = JSON.stringify(,但我怀疑http.post 方法的第二个参数需要一个javascript 对象而不是字符串化的json。你试过了吗? 【参考方案1】:

所以错误是 loopback 3 没有正确类型转换。我必须检查哪个变量没有正确转换,然后在我的中间件中进行相应的解析。

if(req.body.accountId)
    var numberId = parseInt(req.body.accountId);
    req.body.accountId = numberId;

不是一个完美的解决方案,而是一种解决方法!

【讨论】:

以上是关于值不是字符串 - StrongLoop的主要内容,如果未能解决你的问题,请参考以下文章

使用 StrongLoop 自动创建 mysql 表

StrongLoop 验证错误和必填字段

Strongloop:如果 Hook 操作失败,如何返回错误?

苹果swift中Strongloop的环回iOS客户端SDK

centos6 部署strongloop

无法将 strongloop 与 angular.js 集成,远程方法代码未在 lb.services 中生成