简体   繁体   English

Gatsby Node API onPostBuild in gatsby-node.js 无法执行

[英]Gatsby Node API onPostBuild in gatsby-node.js unable to execute

 module.exports = { parseXML: function(file_path, callback) { callback("test 1") readFile(file_path.toString(), "utf-8", function(err, data) { if (err) callback(err); parseString(data, function(err, result) { if (err) callback(err); callback("test 2") var json = result; delete json.sitemapindex.sitemap[1].lastmod; var builder = new xml2js.Builder(); var xml = builder.buildObject(json); write2File(file_path.toString(), xml, function(err, data) { if (err) callback(err); callback("test 3") callback("successfully written our update xml to ",file_path.toString()); }); }); }); callback("test 4") } }

This is a function to parse XML to JSON and modify the JSON and write back to XML. This is a function to parse XML to JSON and modify the JSON and write back to XML. I called this function in gatsby-node.js in onPostBuild like this我在onPostBuildgatsby-node.js中这样称呼这个 function

exports.onPostBuild = async () => {sitemap_mod.parseXML("filename")}

However, in the gatsby build , it only printed out test:1 and test:4 , which means it failed to execute anything in the readFile function.但是,在gatsby build中,它只打印出test:1test:4 ,这意味着它无法执行 readFile function 中的任何内容。 I suppose this is because the onPostBuild is an async function and I need to add some await state in my code.我想这是因为onPostBuild是异步 function ,我需要在我的代码中添加一些await state 。 I applied this function in other node API such as onPostBootstrap and onCreateDevServer and they were both working properly, able to execute readFile and write2File function.我在其他节点 API 中应用了这个 function,例如onPostBootstraponCreateDevServer ,它们都工作正常,能够执行readFilewrite2File ZC1C425268E68385D14AB5074C17A。 So I think there's something wrong about my usage of onPostBuild .所以我认为我对onPostBuild的使用有问题。 Thanks for your help!谢谢你的帮助!

Yes, you should indeed make sure to wait for the function to finish.是的,您确实应该确保等待 function 完成。

You need to wait for the three asynchronous calls you have inside parseXML .您需要等待parseXML内部的三个异步调用。 I usually use promisify to turn them into async functions:我通常使用promisify将它们变成异步函数:

const { promisify } = require("util");
const asyncParseString = promisify(parseString);
const asyncReadFile = promisify(readFile);
const asyncWrite2File = promisify(write2File);

function transformJsonToXml(json) {
  ...
} 

async function parseXML(file_path) {
  const data = await asyncReadFile(file_path.toString(), "utf-8");
  const json = await asyncParseString(data);

  await asyncWrite2File(file_path.toString(), transformJsonToXml(json));
}

Your onPostBuild then needs to wait for parseXML to finish.然后您的onPostBuild需要等待parseXML完成。 So either put an await before the parseXML call or just use所以要么在parseXML调用之前放置一个await ,要么只使用

exports.onPostBuild = () => sitemap_mod.parseXML("filename")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 gatsby-node.js 中执行 graphql 中的所有查询? - How to execute all queries in graphql inside gatsby-node.js? 重构 gatsby-node.js promise - Refactoring gatsby-node.js promise 如何在 Gatsby-node.js 中动态创建多个页面类型 - How to create multiple page types dynamically in Gatsby-node.js 将gatsby-node.js中的JSON数据与发布过程集成 - Integrate JSON data with publishing process in gatsby-node.js Gatsby.js 没有生成 gatsby-node.js 文件的 graphql 查询正确指示它生成的动态页面 - Gatsby.js is not generating the dynamic pages that the gatsby-node.js file's graphql query is properly instructing it to generate 将上下文从 gatsby-node.js 传递到 react 组件(分页/导航链接故障排除) - Passing context from gatsby-node.js into react components (pagination/navigation link troubleshooting) 在创建我的 gatsby-node.js 文件并正确指示通过 graphql 动态生成的页面后,我无法查看任何详细产品 - After creating my gatsby-node.js file with the pages properly instructed to be dynamically generated via graphql, I can't view any of detailed product Gatsby JS,无法在回调中创建节点 - Gatsby JS, Can't Create Node In Callback 当 gatsby 构建图像时,Node JS 崩溃 - Node JS crashes when gatsby builds the images Gatsby - 错误:找不到模块 '..\node_modules\gatsby\dist\utils\babel-loader.js' - Gatsby - Error: Cannot find module '..\node_modules\gatsby\dist\utils\babel-loader.js'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM