Javascript 在Meteor中从mongo返回的对象中存储键的值
好吧,让我说我是流星的新人,我在这里绞尽脑汁。我有一个顾客的收藏品。它有常用的客户字段、地址、城市邮政等 我对这个集合进行了以下调用以返回一条记录Javascript 在Meteor中从mongo返回的对象中存储键的值,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,好吧,让我说我是流星的新人,我在这里绞尽脑汁。我有一个顾客的收藏品。它有常用的客户字段、地址、城市邮政等 我对这个集合进行了以下调用以返回一条记录 var customerRecord = customersColl.find({customerName: suggestion.value}).fetch(); 这将成功返回一个包含我的客户记录的对象。现在我需要做的是将那些值,比如地址、城市邮政等赋给变量。这就是我失败的地方。我尝试过的每一种方法都会出现错误或未定义。如何使以下各项发挥作用:
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);
返回一个文档数组。您似乎希望检索单个文档,因此应改为使用:
find
withfetch
将返回结果数组,而不是单个文档。尝试使用findOne
这很好,我可以改变这一点,在整个过程中,我曾经这样做过一次。但问题仍然存在。如何将返回对象中的值赋给变量。您所做的似乎很好,但如果不知道模式,我无法确定。console.log(customerRecord)
显示了什么?请参见上面的评论
var customerRecord = customersColl.findOne({customerName: suggestion.value});