结果变成“ [对象承诺]”将字符串注入文本编辑器(Tinymce)之后

发布于 2025-01-31 11:09:22 字数 4121 浏览 1 评论 0原文

当该页面首先在路径/书籍/新的页面中渲染时将注入文本编辑器中。但是, [Object Promise] 在文本编辑器中意外显示。它不是一个空字符串吗?为什么它包裹在诺言中?

app.js

  const isAuthorizedByNotion = async (req, res, next) => {
      try {
        res.locals.notionContent = "";
        if (req.session.notionToken && req.query.notionId) {
          const { notionId } = req.query;
          const notion = new Client({
            auth: req.session.notionToken,
          });
          const n2m = new NotionToMarkdown({ notionClient: notion });
          const mdblocks = await n2m.pageToMarkdown(notionId);
          const mdString = n2m.toMarkdownString(mdblocks);
          res.locals.notionContent = JSON.stringify(mdString);
        }
        return next();
      } catch (err) {
        console.dir(err);
      }
    };
    app.engine("ejs", engine);
    app.use(session(sessionConfig));
    app.set("views", path.join(__dirname, "/views"));
    app.set("view engine", "ejs");
    // SERVING STATIC ASSETS
    app.use(express.static(path.join(__dirname, "public")));
    
    app.use(cors());
    app.use(express.urlencoded({ extended: true }));
    app.use(express.json());
    
    app.get("/books/new", isAuthorizedByNotion, (req, res) => {
      res.render("books/new");
    });

ejs

<div class="mb-3">
  <label for="summary" class="form-label">Book Abstract</label>
  <textarea class="form-control" id="summary" rows="4" name="summary"></textarea>
</div>
<script>
  const markdownNotion = < %- notionContent % >
        tinymce.init({
          selector: '#summary',
          resize: false,
          branding: false,
          removed_menuitems: 'print',
          plugins: 'wordcount autosave quickbars link',
          toolbar: "undo redo | blocks | bold italic |alignleft aligncenter alignright alignjustify | indent outdent |import",
          setup: function(editor) {
            editor.ui.registry.addIcon('notion', '<svg height="25" width="25" xmlns="http://www.w3.org/2000/svg" viewBox="12 0.18999999999999906 487.619 510.941"><path d="M96.085 91.118c15.81 12.845 21.741 11.865 51.43 9.884l279.888-16.806c5.936 0 1-5.922-.98-6.906L379.94 43.686c-8.907-6.915-20.773-14.834-43.516-12.853L65.408 50.6c-9.884.98-11.858 5.922-7.922 9.883zm16.804 65.228v294.491c0 15.827 7.909 21.748 25.71 20.769l307.597-17.799c17.81-.979 19.794-11.865 19.794-24.722V136.57c0-12.836-4.938-19.758-15.84-18.77l-321.442 18.77c-11.863.997-15.82 6.931-15.82 19.776zm303.659 15.797c1.972 8.903 0 17.798-8.92 18.799l-14.82 2.953v217.412c-12.868 6.916-24.734 10.87-34.622 10.87-15.831 0-19.796-4.945-31.654-19.76l-96.944-152.19v147.248l30.677 6.922s0 17.78-24.75 17.78l-68.23 3.958c-1.982-3.958 0-13.832 6.921-15.81l17.805-4.935V210.7l-24.721-1.981c-1.983-8.903 2.955-21.74 16.812-22.736l73.195-4.934 100.889 154.171V198.836l-25.723-2.952c-1.974-10.884 5.927-18.787 15.819-19.767zM42.653 23.919l281.9-20.76c34.618-2.969 43.525-.98 65.283 14.825l89.986 63.247c14.848 10.876 19.797 13.837 19.797 25.693v346.883c0 21.74-7.92 34.597-35.608 36.564L136.64 510.14c-20.785.991-30.677-1.971-41.562-15.815l-66.267-85.978C16.938 392.52 12 380.68 12 366.828V58.495c0-17.778 7.922-32.608 30.653-34.576z" fill-rule="evenodd"/></svg>')
            editor.ui.registry.addButton("import", {
              icon: 'notion',
              tooltip: 'Import from Notion',
              onAction: function(_) {
                const width = 800;
                const height = 800;
                const left = (screen.width / 2) - (width / 2);
                const top = (screen.height / 2) - (height / 2);
                window.open('https://api.notion.com/v1/oauth/authorize?owner=user&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&response_type=code', "_blank", `toolbar=yes,scrollbars=yes,resizable=yes,top=${top},left=${left},width=${width},height=${height}`);
              }
            })
            editor.on('init', function(e) {
              editor.setContent(markdownNotion)
            })
          }
        });
</script>

When the page is first rendered in the path /books/new, notionContent variable stored in res.locals should be an empty string such that nothing will be injected into the text editor. However, [object Promise] is unexpectedly shown in the text editor. Shouldn't it be an empty string? Why is it wrapped in promise?

app.js

  const isAuthorizedByNotion = async (req, res, next) => {
      try {
        res.locals.notionContent = "";
        if (req.session.notionToken && req.query.notionId) {
          const { notionId } = req.query;
          const notion = new Client({
            auth: req.session.notionToken,
          });
          const n2m = new NotionToMarkdown({ notionClient: notion });
          const mdblocks = await n2m.pageToMarkdown(notionId);
          const mdString = n2m.toMarkdownString(mdblocks);
          res.locals.notionContent = JSON.stringify(mdString);
        }
        return next();
      } catch (err) {
        console.dir(err);
      }
    };
    app.engine("ejs", engine);
    app.use(session(sessionConfig));
    app.set("views", path.join(__dirname, "/views"));
    app.set("view engine", "ejs");
    // SERVING STATIC ASSETS
    app.use(express.static(path.join(__dirname, "public")));
    
    app.use(cors());
    app.use(express.urlencoded({ extended: true }));
    app.use(express.json());
    
    app.get("/books/new", isAuthorizedByNotion, (req, res) => {
      res.render("books/new");
    });

EJS

<div class="mb-3">
  <label for="summary" class="form-label">Book Abstract</label>
  <textarea class="form-control" id="summary" rows="4" name="summary"></textarea>
</div>
<script>
  const markdownNotion = < %- notionContent % >
        tinymce.init({
          selector: '#summary',
          resize: false,
          branding: false,
          removed_menuitems: 'print',
          plugins: 'wordcount autosave quickbars link',
          toolbar: "undo redo | blocks | bold italic |alignleft aligncenter alignright alignjustify | indent outdent |import",
          setup: function(editor) {
            editor.ui.registry.addIcon('notion', '<svg height="25" width="25" xmlns="http://www.w3.org/2000/svg" viewBox="12 0.18999999999999906 487.619 510.941"><path d="M96.085 91.118c15.81 12.845 21.741 11.865 51.43 9.884l279.888-16.806c5.936 0 1-5.922-.98-6.906L379.94 43.686c-8.907-6.915-20.773-14.834-43.516-12.853L65.408 50.6c-9.884.98-11.858 5.922-7.922 9.883zm16.804 65.228v294.491c0 15.827 7.909 21.748 25.71 20.769l307.597-17.799c17.81-.979 19.794-11.865 19.794-24.722V136.57c0-12.836-4.938-19.758-15.84-18.77l-321.442 18.77c-11.863.997-15.82 6.931-15.82 19.776zm303.659 15.797c1.972 8.903 0 17.798-8.92 18.799l-14.82 2.953v217.412c-12.868 6.916-24.734 10.87-34.622 10.87-15.831 0-19.796-4.945-31.654-19.76l-96.944-152.19v147.248l30.677 6.922s0 17.78-24.75 17.78l-68.23 3.958c-1.982-3.958 0-13.832 6.921-15.81l17.805-4.935V210.7l-24.721-1.981c-1.983-8.903 2.955-21.74 16.812-22.736l73.195-4.934 100.889 154.171V198.836l-25.723-2.952c-1.974-10.884 5.927-18.787 15.819-19.767zM42.653 23.919l281.9-20.76c34.618-2.969 43.525-.98 65.283 14.825l89.986 63.247c14.848 10.876 19.797 13.837 19.797 25.693v346.883c0 21.74-7.92 34.597-35.608 36.564L136.64 510.14c-20.785.991-30.677-1.971-41.562-15.815l-66.267-85.978C16.938 392.52 12 380.68 12 366.828V58.495c0-17.778 7.922-32.608 30.653-34.576z" fill-rule="evenodd"/></svg>')
            editor.ui.registry.addButton("import", {
              icon: 'notion',
              tooltip: 'Import from Notion',
              onAction: function(_) {
                const width = 800;
                const height = 800;
                const left = (screen.width / 2) - (width / 2);
                const top = (screen.height / 2) - (height / 2);
                window.open('https://api.notion.com/v1/oauth/authorize?owner=user&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&response_type=code', "_blank", `toolbar=yes,scrollbars=yes,resizable=yes,top=${top},left=${left},width=${width},height=${height}`);
              }
            })
            editor.on('init', function(e) {
              editor.setContent(markdownNotion)
            })
          }
        });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文