首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换为字符串失败的值\"{详细信息:'233‘}\“路径\"setno\”模型\"Trains\

转换为字符串失败的值\"{详细信息:'233‘}\“路径\"setno\”模型\"Trains\
EN

Stack Overflow用户
提问于 2020-11-04 04:39:44
回答 1查看 149关注 0票数 1

我一直在努力看并解决这一问题,但最终却没有找到任何相关的解决办法。需要更多的投入和建议来解决这个问题。请检查我下面的代码详细信息。我使用的是Express GraphQL,和猫鼬版本:^5.9.25

我用GraphQL来做这个,

下面的

是我的查询请求

代码语言:javascript
复制
query {
  getTrainsbySetno(details:"233"){
  _id
  setno
  train_no
  start_station_code
  end_station_code
  route_code
  train_type
  start_on
  change_on
  halts_at
  createdAt
  updatedAt
 }
}

My GraphqL查询响应

代码语言:javascript
复制
{
  "errors": [
  {
  "message": "Cast to string failed for value \"{ details: '233' }\" at path \"setno\" for model \"Trains\"",
  "locations": [
    {
      "line": 55,
      "column": 3
    }
  ],
  "path": [
    "getTrainsbySetno"
  ]
}
],
"data": {
 "getTrainsbySetno": null
 }
}

下面的

是我的Resolver

代码语言:javascript
复制
getTrainsbySetno: async (details) => {
     
    try {
        const result = await Trains.find({$or:[{setno: details },{train_no: details}]});
        console.log(result);
        return result.map(res => {
           
            return { ...res._doc, _id: res.id };
            
        });            
    } catch (err) {
        
        throw err;
    }
   
},

下面是我的模式:

代码语言:javascript
复制
const Schema = mongoose.Schema;

const trainsSchema = new Schema ({

        setno : {
            type: String,
            required: true
        },
        train_no : {
            type: String,
            required: true
        },
        start_station_code : {
            type: String,
            required: false
        },
        end_station_code : {
            type: String,
            required: true
        },
        route_code : {
            type: String,
            required: true
        },
        train_type : {
            type: String,
            required: true
        },
        start_on : {
            type: String,
            required: true
        },
        change_on : {
            type: String,
            required: true
        },
        halts_at : {
            type: String,
            required: true
        }
  
    },
    { timestamps: true }
);

module.exports = mongoose.model('Trains', trainsSchema);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-04 06:39:20

在getTrainsbySetno方法中,参数"details“是一个对象。因此,要获得details obj的值,我们需要用"details.details“替换细节。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64674333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档