【发布时间】:2020-11-13 04:04:21
【问题描述】:
我有以下代码,当我尝试获取this.userId 时,我似乎无法将值输入到数据库中。它在前端通过 pub sub 不知道如何解决这个问题,如果我使用 Meteor.userId() 我得到一个错误,说它不能在发布函数中使用。
import { Meteor } from "meteor/meteor";
import { Accounts } from "meteor/accounts-base";
import {
LeadsCollection,
LeadsBuilderCollection,
} from "/imports/api/LeadsCollection";
import "/imports/api/leadsMethods";
import "/imports/api/leadsPublications";
const insertLead = (leadEmail) =>
LeadsCollection.insert({
email: leadEmail,
createdAt: new Date(),
userId: this.userId,
});
const insertLeadBuilderType = (leadsBuilder) =>
LeadsBuilderCollection.insert({ type: leadsBuilder });
const SEED_USERNAME = "admin";
const SEED_PASSWORD = "admin";
Meteor.startup(() => {
if (!Accounts.findUserByUsername(SEED_USERNAME)) {
Accounts.createUser({
username: SEED_USERNAME,
password: SEED_PASSWORD,
});
}
if (LeadsCollection.find().count() === 0) {
[
"First Lead",
"Second Lead",
"Third Lead",
"Fourth Lead",
"Fifth Lead",
"Sixth Lead",
"Seventh Lead",
].forEach(insertLead);
}
if (LeadsBuilderCollection.find().count() === 0) {
["Showroom Lead", "Phone Call Lead", "Website Lead"].forEach(
insertLeadBuilderType
);
}
});
【问题讨论】:
-
一个旁注。您应该使用 Meteor.settings 作为种子用户名和默认密码。更好的是省略密码并发送邀请电子邮件。
-
值得一提的是,现在它只是在开发中,但我会在与真人进行测试之前添加它
标签: javascript mongodb meteor