gpt4 book ai didi

javascript - FlatBuffers 未定义

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:13 24 4
gpt4 key购买 nike

我正在编写一个使用 Google FlatBuffers 的 NodeJs 应用程序。

我在我的 macbook pro 上安装了 flatc 并编译了以下架构:

命名空间 MyAlcoholist;

table Drink {
drink_type_name: string;
drink_company_name: string;
drink_brand_name: string;
drink_flavor_type_name : string;
liquid_color_type_name : string;
liquid_color_is_transparent : bool;
alcohol_vol : float;
calories_for_100g : uint;
global_image_id: ulong;
drink_flavor_id: ulong;
}

table Drinks { drinks:[Drink]; }

root_type Drinks;

架构文件名为 Drink.fbs,它生成了一个名为 Drink_generate.js 的 JavaScript 文件

我将此文件包含在我的 Nodejs 应用程序中,并使用以下代码向其中添加数据。这是我的 flatBufferUtil.js 实用程序文件。

var flatbuffers = require('../js/flatbuffers').flatbuffers;
var builder = new flatbuffers.Builder();

var drinks = require('../fbs/drinks_generated').MyAlcoholist; // Generated by `flatc`.

function drinkArrayToBuffer(drinkArray) {
var drinksVectArray = [];
drinkArray.forEach(function (element, index, array) {
var drinkObj = element;
var drinkBrandName = builder.createString(drinkObj.drink_brand_name);
var drinkCompanyName = builder.createString(drinkObj.drink_company_name);
var drinkflavorTypeName = builder.createString(drinkObj.drink_flavor_type_name);
var drinkTypeName = builder.createString(drinkObj.drink_type_name);
var liquidColorTypeName = builder.createString(drinkObj.liquid_color_type_name);
drinks.Drink.startDrink(builder);
drinks.Drink.addAlcoholVol(builder, drinkObj.alcohol_vol);
drinks.Drink.addCaloriesFor100g(builder,drinkObj.calories_for_100g);
drinks.Drink.addDrinkBrandName(builder,drinkBrandName);
drinks.Drink.addDrinkCompanyName(builder,drinkCompanyName);
drinks.Drink.addDrinkFlavorId(builder,drinkObj.drink_flavor_id);
drinks.Drink.addDrinkFlavorTypeName(builder, drinkflavorTypeName);
drinks.Drink.addDrinkTypeName(builder,drinkTypeName);
drinks.Drink.addGlobalImageId(builder,drinkObj.global_image_id);
drinks.Drink.addLiquidColorIsTransparent(builder,drinkObj.is_transparent);
drinks.Drink.addLiquidColorTypeName(builder,liquidColorTypeName);
var drink = drinks.Drink.endDrink(builder);
drinksVectArray.push(drink);
})
var drinksVect = drinks.createDrinksVector(builder,drinksVectArray);
builder.finish(drinksVect);
var buf = builder.dataBuffer();
return buf;
}

module.exports.drinkArrayToBuffer=drinkArrayToBuffer;

现在,当我执行此函数时,它会失败并出现错误flatbuffers is not Defined

我调试了我的代码,发现它文件在以下代码行中:

    drinks.Drink.addDrinkFlavorId(builder,drinkObj.drink_flavor_id);

如果我进入 addDrinkFlavorId 函数,我会在 Drinks_generted.js 中看到以下代码:

MyAlcoholist.Drink.addDrinkFlavorId = function(builder, drinkFlavorId) {
builder.addFieldInt64(9, drinkFlavorId, flatbuffers.Long.ZERO);
};

如您所见,它使用 flatbuffers.Long.ZERO 但该文件中根本没有定义 flatbuffers。编译没有提供任何错误,那么我错过了什么?

最佳答案

在我看来,这是一个错误...生成的文件似乎是根据 flatbuffers 要求独立存在的。但是,对于自定义 flatbuffers.Long 类,默认值 flatbuffers.Long.ZERO 会渗入生成的文件中。

虽然这并不是一个解决方案,但一种解决方法是手动将所需的 FlatBuffers 添加到生成的文件中;它很丑陋,但可能比在更合适的答案(或修复)出现之前被阻止要好。

// In `drinks_generated.js`
var flatbuffers = require('../js/flatbuffers').flatbuffers;

注意:

drinks.Drink.addDrinkFlavorId()drinks.Drink.addGlobalImageId() 函数期望将 flatbuffers.Long 值传递给它们,因为它们在架构(fbs 文件)中被指定为 ulong。因此,您需要确保您没有尝试传递简单的 number 类型。

例如:

var my_long = flatbuffers.Long(100, 0); // low = 100, high = 0
drinks.Drink.addDrinkFlavorId(builder, my_long);

因此,另一种可能的解决方法是更改​​架构中这些字段的数据类型,以避免使用 ulong,直到更清楚这里发生的情况为止。

附注我非常确定该代码段第 30 行上的 drinks.createDrinksVector 应该是 drinks.Drinks.createDrinksVector

关于javascript - FlatBuffers 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com