javascript - 将 mongo 返回的对象中的键值存储在 Meteor 中

标签 javascript mongodb meteor

好吧,我只想说我是 meteor 新手,正在绞尽脑汁。我有一位客户声明的收藏品。它有常见的客户字段、地址、城市、邮政等。

我对此集合进行以下调用以返回一条记录。

var customerRecord = customersColl.find({customerName: suggestion.value}).fetch();

这成功返回了一个包含我的客户记录的对象。现在我需要做的是将地址、城市邮政等这些值分配给变量。这就是我失败的地方。我尝试过的每一种方法都会出现错误或未定义。我如何让以下内容发挥作用:

var customerAddress = customerRecord.customerAddress.value;

这是我用于该对象的方案。

customersColl.attachSchema(new SimpleSchema({
    customerName:{
    type: String,
    label: "Customer:"
    },
    customerSuite: {
        type: String,
        optional: true,
        label: "Suite:"
    },
    customerAddress: {
        type: String,
        label: "Address:"
    },
    customerCity: {
        type: String,
        label: "City:"
    },
    customerState: {
        type: String,
        label: "State/Prov:"
    },
    customerZip: {
        type: String,
        label: "Zip/Postal"
    },
    customerPhone1: {
        type: String,
        label: "Phone 1:"
    },
    customerPhone2: {
        type: String,
        optional: true,
        label: "Phone 2:"
    },
    customerFax: {
        type: String,
        optional: true,
        label: "Fax:"
    },
    customerFactorInvoices: {
        type: Boolean,
        optional: true,
        label: "Factor Invoicing to Payment Company"
    },
    factoringName: {
        type: String,
        optional: true,
        label: "Company:"
    },
    factoringSuite: {
        type: String,
        optional: true,
        label: "Suite:"
    },
    factoringAddress: {
        type: String,
        optional: true,
        label: "Address:"
    },
    factoringCity: {
        type: String,
        optional: true,
        label: "City:"
    },  
    factoringState: {
        type: String,
        optional: true,
        label: "State/Prov:"
    },      
    factoringZip: {
        type: String,
        optional: true,
        label: "Zip/Postal:"
    },      
    factoringPhone1: {
        type: String,
        optional: true,
        label: "Phone 1:"
    },      
    factoringPhone2: {
        type: String,
        optional: true,
        label: "Phone 2:"
    },      
    factoringFax: {
        type: String,
        optional: true,
        label: "Fax:"
    },      
    factoringEmail: {
        type: String,
        regEx: SimpleSchema.RegEx.Email,
        optional: true,
        label: "Email:"
    },      
    customerOriginalInvoices: {
        type: Boolean,
        optional: true,
        label: "Require Original Invoices Mailed"
    },      
    customerAccountHold: {
        type: Boolean,
        optional: true,
        label: "Account on Hold"
    },      
    createdDate: {
        type: Date
    },      
    createdUser: {
        type: String,
    },  
    emailRoles1: {
        type: Array,
          label: "Email Roles",
          optional: true,
          autoform: {
             options: [
                {
                   label: "On Order Create",
                   value: "onOrderCreate"
                },
                {
                   label: "On PU Del",
                   value: "onPuDel"
                },
                {
                   label: "Invoicing",
                   value: "invoicing"
                },
                {
                   label: "Accounts Payable",
                   value: "accountsPayable"
                }
             ]
          }
      },
       "emailRoles1.$": {
          type: String,
          label: "Email Roles",
          optional: true
       },
    emailRoles2: {
        type: Array,
          label: "Email Roles",
          optional: true,
          autoform: {
             options: [
                {
                   label: "On Order Create",
                   value: "onOrderCreate"
                },
                {
                   label: "On PU Del",
                   value: "onPuDel"
                },
                {
                   label: "Invoicing",
                   value: "invoicing"
                },
                {
                   label: "Accounts Payable",
                   value: "accountsPayable"
                }
             ]
          }
      },
       "emailRoles2.$": {
          type: String,
          label: "Email Roles",
          optional: true
       },
    emailRoles3: {
        type: Array,
          label: "Email Roles",
          optional: true,
          autoform: {
             options: [
                {
                   label: "On Order Create",
                   value: "onOrderCreate"
                },
                {
                   label: "On PU Del",
                   value: "onPuDel"
                },
                {
                   label: "Invoicing",
                   value: "invoicing"
                },
                {
                   label: "Accounts Payable",
                   value: "accountsPayable"
                }
             ]
          }
      },
       "emailRoles3.$": {
          type: String,
          label: "Email Roles",
          optional: true
       },
    emailRoles4: {
        type: Array,
          label: "Email Roles",
          optional: true,
          autoform: {
             options: [
                {
                   label: "On Order Create",
                   value: "onOrderCreate"
                },
                {
                   label: "On PU Del",
                   value: "onPuDel"
                },
                {
                   label: "Invoicing",
                   value: "invoicing"
                },
                {
                   label: "Accounts Payable",
                   value: "accountsPayable"
                }
             ]
          }
      },
       "emailRoles4.$": {
          type: String,
          label: "Email Roles",
          optional: true
       },
    customerEmail1: {
        type: String,
        regEx: SimpleSchema.RegEx.Email,
        optional: true,
        label: "Email 1:"
    },
    customerEmail2: {
        type: String,
        regEx: SimpleSchema.RegEx.Email,
        optional: true,
        label: "Email 2:"
    },
    customerEmail3: {
        type: String,
        regEx: SimpleSchema.RegEx.Email,
        optional: true,
        label: "Email 3:"
    },
    customerEmail4: {
        type: String,
        regEx: SimpleSchema.RegEx.Email,
        optional: true,
        label: "Email 4:"
    },      
}));

这不起作用,但我尝试过的任何事情都没有起作用。对此的任何帮助将不胜感激。

更正代码

这是现在可与 findOne 配合使用的正确代码。谢谢大家的帮助。

        var customerRecord =  customersColl.findOne({customerName: suggestion.value});
        console.log(customerRecord.customerAddress);

最佳答案

fetch返回文档数组。您似乎期望检索单个文档,因此您应该使用 findOne相反:

var customerRecord = customersColl.findOne({customerName: suggestion.value});

关于javascript - 将 mongo 返回的对象中的键值存储在 Meteor 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516422/

相关文章:

mongodb - 如何在 Golang 中为 Mongodb 使用 GORM?

java - mongodb java 检查插入是否成功

javascript - 如何在 Meteor 中将图像资源编码为 Base64 数据 URI?

javascript - 从字符串中删除未定义的内容

javascript - Meteor JS 全局函数

javascript - 将 javascript post 函数转换为 Angular js

python - 你在 MongoDB 中使用数据映射器吗?

javascript - 如何发布具有不同数据子集的同一集合?

javascript - Meteor.Call 奇怪的行为

javascript - 如何在Jquery的方法中管理具有相同类的元素